The difference between line separator and paragraph separator

Did you know that there is a possible difference between line and paragraph separators with unicode characters?

This is very interesting when you are working with attributed strings. Basically, a \n is a paragraph separator (same as unicode character U+2029), lineSpacing doesn’t have any impact on this “line break“. You must use unicode line separator (U+2028) instead.

// Let's prepare our paragraph style attribute
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 4.0f; // For line separators and automatic line breaks
paragraphStyle.paragraphSpacing = 20.0f; // For paragraph separators

// Now we can use it to create an attributed string that behave differently with line and paragraph separators
[[NSAttributedString alloc] initWithString:@"Hello, World!"
attributes:@{ NSParagraphStyleAttributeName: paragraphStyle }];

More information about this in Apple iOS documentation

Leave a Reply

Your email address will not be published. Required fields are marked *