Last active
January 7, 2016 03:02
-
-
Save phucnm/fbabf2213809e11174c6 to your computer and use it in GitHub Desktop.
Store and retrieve NSAttributedString or NSMutableAttributedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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