Debugging NSAttributedString Using a Block to Print Attributes

In a post earlier this week, Create NSAttributedString from HTML in iOS 7, I outlined the basics for reading an HTML file and generating an NSAttributedString. This post includes a few tips for debugging an NSAttributedString.

Assuming you have an html file with the name test.html, the code for creating the NSAttributedString is as follows:

// Create URL from HTML file in application bundle
NSURL *html = [[NSBundle mainBundle] URLForResource: @"test" withExtension:@"html"];
 
// Create attributed string from HTML
NSAttributedString *attrStr = [[NSAttributedString alloc]
  initWithFileURL:html
  options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
  documentAttributes:nil error:nil];

Debugging NSAttributedStrings

Should the output of the NSAttributedString not be what you expected, as is most always the case, debug information can provide some insight.

Printing a list of the attributes that make up an NSAttributedString is a walk in the park when using the following method, which uses block syntax:

- (void)enumerateAttributesInRange:(NSRange)enumerationRange 
  options:(NSAttributedStringEnumerationOptions)opts 
  usingBlock:(void (^)(NSDictionary *attrs, NSRange range, BOOL *stop))block

To wrap all this together into an example that you can see both the output of the NSAttributedString in a UITextView, as well as view the debug information, copy/paste the code below into a project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Create URL from HTML file in application bundle
NSURL *html = [[NSBundle mainBundle] URLForResource: @"test" withExtension:@"html"];
 
// Create attributed string from HTML
NSAttributedString *attrStr = [[NSAttributedString alloc]
  initWithFileURL:html
  options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
  documentAttributes:nil error:nil];
 
// Create textview, add attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 320, 50)];
[textView setAttributedText:attrStr];
 
// Show textview
[[self view] addSubview:textView];
 
// Debug information for the NSAttributedString
NSRange range = NSMakeRange (0, attrStr.length);
[attrStr enumerateAttributesInRange: range
   options: 0
   usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop)
   {
      NSLog (@"range: %@  attributes: %@", NSStringFromRange(range), attributes);
   }
];

And here’s the HTML that I used in the example:

<div style="background-color:#F1F1F1; font-size:14px; color:#304182; 
  text-align:center; margin-left:10px; padding-right:10px"> 
<p>iOS <span style="font-size:18px; color:#E88834;">Developer</span> Tips</p>
</div>

The display output is shown below:

NSAttributedString from HTML

A partial listing of the debug data follows, showing how the attributes of an NSAttributedString are broken down:

2014-01-10 15:08:00.413 Sandbox[62794:70b] range: {0, 4}  attributes: {
    NSBackgroundColor = "UIDeviceRGBColorSpace 0.945098 0.945098 0.945098 1";
    NSColor = "UIDeviceRGBColorSpace 0.188235 0.254902 0.509804 1";
    NSFont = "<UICTFont: 0xa1d0580> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 14, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0.188235 0.254902 0.509804 1";
    NSStrokeWidth = 0;
}
2014-01-10 15:08:00.414 Sandbox[62794:70b] range: {4, 9}  attributes: {
    NSBackgroundColor = "UIDeviceRGBColorSpace 0.945098 0.945098 0.945098 1";
    NSColor = "UIDeviceRGBColorSpace 0.909804 0.533333 0.203922 1";
    NSFont = "<UICTFont: 0xa28b3b0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 18.00pt";
    NSKern = 0;
    NSParagraphStyle = "Alignment 1, LineSpacing 0, ParagraphSpacing 14, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSStrokeColor = "UIDeviceRGBColorSpace 0.909804 0.533333 0.203922 1";
    NSStrokeWidth = 0;
}

Additional NSAttributedString Reading

If you are looking for more information about debugging NSAttributedStrings or working with attributed strings in general, I recommend you start with the NSAttributedString Class Reference. Next, take a look at the documentation for the method above enumerateAttributesInRange.

You can find a few few examples of creating NSAttributedString objects on Stackoverflow.

On the posts below I walk through how to create NSAttributedString objects using a range of attributes, including font, strikethrough, foreground/background colors, among others:

NSAttributedString, Set Text Font, Foreground And Background Colors, StrikeThrough And Shadow

NSAttributedString: Set Text Underline, Paragraph Styles and Glyph Form