Skip to content

Instantly share code, notes, and snippets.

@phucnm
Last active January 7, 2016 03:02
Show Gist options
  • Save phucnm/fbabf2213809e11174c6 to your computer and use it in GitHub Desktop.
Save phucnm/fbabf2213809e11174c6 to your computer and use it in GitHub Desktop.
Store and retrieve NSAttributedString or NSMutableAttributedString
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Hello, world!"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(3, 3)];
NSData *data = [string dataFromRange:NSMakeRange(0, string.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
error:nil];
NSAttributedString *string2 = [[NSAttributedString alloc] initWithData:data
options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil
error:nil];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment