Skip to content

Instantly share code, notes, and snippets.

@danielnorton
danielnorton / AttributedRoundtrip.m
Last active August 9, 2016 13:56
Unexpected roundtrip of HTML through NSAttributedString. This demonstrates a simple HTML file going into an NSAttributedString and then writing that string back out to a new HTML file. The output file is structured very differently than the input file. This was discovered while experimenting with using a UITextView as a rich text editor.
- (NSAttributedString *)loadContent {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"input" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *options = @{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
};