Skip to content

Instantly share code, notes, and snippets.

@pita5
Created January 10, 2012 16:46
Show Gist options
  • Save pita5/1589961 to your computer and use it in GitHub Desktop.
Save pita5/1589961 to your computer and use it in GitHub Desktop.
@interface MWPersistableAttributedString : NSMutableAttributedString <NSCoding>
@end
@implementation MWPersistableAttributedString
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:[self string] forKey:@"string"];
NSMutableArray *attributes = [NSMutableArray array];
[self enumerateAttributesInRange:NSMakeRange(0, [self length])
options:0
usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop)
{
// This does nothing, nothing to see here....
NSLog(@"range %@", NSStringFromRange(range));
NSLog(@"attrs %@", attrs);
}];
[aCoder encodeObject:attributes forKey:@"attributes"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [self initWithString:[aDecoder decodeObjectForKey:@"string"]]))
{
for (NSDictionary *attributes in [aDecoder decodeObjectForKey:@"attributes"])
{
[self addAttributes:nil
range:NSMakeRange(0, 0)];
}
}
return self;
}
@end
NSMutableAttributedString *mutText = [[[MWPersistableAttributedString alloc] initWithString:@"hello world"] autorelease];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment