3

I have NSAttributedString with NSTextAttachment. I've created subclass of NSTextAttachment (with implemented encode/decode methods) and it has property attachedView so I can put any view as NSAttachment into attributed string. The problem that I cannot store and retrieve attachment to/from UIPateboard.

Any suggestions how it can be done?

//Store
NSData *data = [[self.storage attributedSubstringFromRange:self.selectedRange] archivedData];
[[UIPasteboard generalPasteboard] setItems:@[[(id)kUTTypePlainText: attributedString.string],
                                             [(id)kUTTypeData: data]]];

//Retrieve
NSAttributedString *attributedString = [[UIPasteboard generalPasteboard] loadAttributedString];
if (!attributedString) {
    [super paste:sender];
    return;
}

//Extension
@implementation NSAttributedString (Additions)

- (NSData *)archivedData {
    return [NSKeyedArchiver archivedDataWithRootObject:self];
}

- (NSAttributedString *)unarchiveWithData:(NSData *)data {
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end
landonandrey
  • 1,091
  • 1
  • 14
  • 25

0 Answers0