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

Wed, Dec 12

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:

//-----------------------------
// Create attributed string
//-----------------------------
NSString *str = @"MobileDeveloperTips.com";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
 
// Add attribute NSUnderlineStyleAttributeName
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(6, 9)];
 
// Set background color for entire range
[attributedString addAttribute:NSBackgroundColorAttributeName
  value:[UIColor colorWithRed:0.103 green:0.305 blue:0.492 alpha:1.000]
  range:NSMakeRange(0, [attributedString length])];
 
// Define label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 80)];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
 
// Set label text to attributed string
[label setAttributedText:attributedString];
[[self view] addSubview:label];
NSParagraphStyleAttributeName Examples

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//-----------------------------
// Create attributed string
//-----------------------------
NSString *str = @"We drink to your coffin. May it be built from the wood of a hundred year old oak tree that I shall plant tomorrow.";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
 
// Create NSMutableParagraphStyle object
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentRight;
 
// Add attribute NSParagraphStyleAttributeName
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [attributedString length])];
 
// Define label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 240, 120)];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:0];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
 
// Set label text to attributed string
[label setAttributedText:attributedString];
[[self view] addSubview:label];

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:

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
paragraph.hyphenationFactor = .9;

The output now looks as follows:

NSVerticalGlyphFormAttributeName

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;

One comment

great examples, thx!

by tacotom on Dec 12, 2012. Reply #

Leave a Comment