3

The String Programming Guide in the iOS Developer Library mentions "Line and Paragraph Separator Characters": NSParagraphSeparatorCharacter and NSLineSeparatorCharacter. But Xcode is not accepting them (iOS7 is the target):

NSLog(@"This is%Con two lines…", (unichar)NSLineSeparatorCharacter);

Code is from this SE post

Is this a documentation error and they're only available on OSX? If so, is there any way to achieve the same thing (new line within the same paragraph) using TextKit on iOS7?

Community
  • 1
  • 1
user1459524
  • 3,613
  • 4
  • 19
  • 28

4 Answers4

2

Just define it yourself,

#define NSLineSeparatorCharacter 0x2028
unichar newLine = NSLineSeparatorCharacter;
[descriptionText appendString:[NSString stringWithCharacters:&newLine length:1]];
ajmccall
  • 2,024
  • 23
  • 42
cwehrung
  • 1,286
  • 13
  • 18
  • NSAttributedString with "\u{2028}" in Swift works for me in iOS 10+ to create new lines rather than a new paragraph – awulf Nov 07 '17 at 00:18
1

To bring these constants from macOS to iOS, one can define:

let NSNewLineCharacter = "\u{000a}"
let NSLineSeparatorCharacter = "\u{2028}"
let NSParagraphSeparatorCharacter = "\u{2029}"
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
0

This is defined in NSText.h . It declares the most general programmatic interface for objects that manage text.

This is only for OS X, not iOS. So you can't use those constants in iOS.

To do this in iOS,There are a number of ways in which a line or paragraph break may be represented. Historically \n, \r, and \r\n have been used.

Unicode defines an unambiguous paragraph separator, U+2029 (for which Cocoa provides the constant NSParagraphSeparatorCharacter), and an unambiguous line separator, U+2028 (for which Cocoa provides the constant NSLineSeparatorCharacter).

See more details here :iOS Developer Library: Paragraphs and Line Breaks

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • Right. So it's included in the iOS Documentation Library by mistake then, I guess? How do I achieve the same effect in iOS? – user1459524 Jan 18 '14 at 20:13
  • what do you want to acheive? – Shamsudheen TK Jan 18 '14 at 20:19
  • 1
    What I am trying to achieve is stated in my question: a new line within the same paragraph. Think
    vs.

    in html-speak. I have tried \n and \r and also @"\u2028" but it always creates a new paragraph. BTW, the exact same link you posted is in my original question. That article has in fact caused the question, because it mentions NSLineSeparatorCharacter in the first place for iOS.

    – user1459524 Jan 18 '14 at 22:11
  • where is NSLineSeparatorCharacter? couldn't find in iOS SDK. – Motti Shneor Apr 16 '19 at 17:20
  • You also forgot the [NSCharacterSet newlineCharacterSet] which encapsulate for many uses this set of line separators – Motti Shneor Apr 16 '19 at 17:21
0

Here is a code sample that does what you want. My case was this: I wanted to add log-messages into a log-file, where each log line is terminated by \n (that's how my logger works). However - I sometimes need to have multi-line messages in the log, so they cannot contain \n - or they'll break the log file format. So... Here I replace all the "newlines" with "NSLineSeparatorCharacter":

NSString *logMessage // multi-paragraph string message containing all kinds of line and paragraph breaks.
NSString *singleParagraphMultiLineMessage = 
   [[logMessage componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] 
   componentsJoinedByString:@"\u2028"];

Attention: If you try to display this string via its description, it will change the NSLineSeparator back to \n

For example: if you'll try to use:

NSString *test = [NSString stringWithFormat:"%@", singleParagraphMultiLineMessage];

NSLog(@"%@", singleParagraphMultiLineMessage);

or even po singleParagraphMultiLineMessage in the debugger -

the result will no longer include NSLineSeparatorCharacter. Only if you export it to NSData and save to file - or else extract its contents in UTF8 - you'll see that it works fine. It took me a while to understand why this was happening.

Just to make the answer complete, the NSLineSeparatorCharacter is defined, part of the following enumerator, in Appkit/NSText.h, like thus:

/* Various important Unicode code points */
enum {
    NSEnterCharacter                = 0x0003,
    NSBackspaceCharacter            = 0x0008,
    NSTabCharacter                  = 0x0009,
    NSNewlineCharacter              = 0x000a,
    NSFormFeedCharacter             = 0x000c,
    NSCarriageReturnCharacter       = 0x000d,
    NSBackTabCharacter              = 0x0019,
    NSDeleteCharacter               = 0x007f,
    NSLineSeparatorCharacter        = 0x2028, // <-- here it is.
    NSParagraphSeparatorCharacter   = 0x2029
};

But NOT in UIKit/NSText.h which is miserably empty in comparison with its MacOS counterpart. I wonder why apple neglected iOS developers so badly with this one. Maybe they don't favor NSText, and want developers to move away from using it?. Still - you're welcome to copy that enum, and paste it into your code, thus to achieve MacOS/iOS code-compatibility.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24