iOS 6 NSAttributedString: Set Text Underline, Paragraph Styles and Glyph Form – Part 2

In Part 1 of working with attributed strings in iOS 6 I covered how to use foreground and background colors, specify a preferred font, create character strike-through and show a shadow on text. The specific attribute names that were covered follow:

NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSFontAttributeName;

In this post I will cover how to use underline, paragraph and glyph attributes:

NSString *const NSUnderlineStyleAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;

NSUnderlineStyleAttributeName Example

With the underline attribute we can create text that looks as follows:

The code to underline text is shown below:

NSParagraphStyleAttributeName Examples

The following code will use the paragraph attribute to right align text within a label, creating output that looks as follows:

Let’s change up the paragraph attribute to center text and also to specify a hyphenation ratio, in which hyphenation will occur if the ratio of the text width before hyphenation and the text as hyphenated is less than the specified hyphenation ratio.

Replace lines 8 – 9 above with the following:

The output now looks as follows:


From the Apple documentation:

The value of this attribute is an NSNumber object containing an integer. The value 0 indicates horizontal text. The value 1 indicates vertical text.

Unfortunately, the next sentence is:

In iOS, horizontal text is always used and specifying a different value is undefined.

If this should change in a future version of iOS, I’ll provide an example.

In Part 3 of this series I will cover the remaining NSAttributedString attributes defined in iOS 6:

NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;

Comments are closed.