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.

Continue reading

Create NSAttributedString from HTML in iOS 7

With the release of iOS 7, Apple introduced the method below in the NSAttributedString UIKit Additions:

initWithFileURL:options:documentAttributes:error:

With this little gem you can create an NSAttributedString from data retrieved via URL, and using a document attribute of NSDocumentTypeDocumentAttribute, generate the string from HTML.

Let’s look at an example:

Continue reading

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;
Continue reading