我有NSAttributedString
和NSTextAttachment
。我已经创建了NSTextAttachment
的子类(具有已实现的编码/解码方法),并且它具有属性attachedView
,因此我可以将NSAttachment
的任何视图放入属性字符串中。我无法存储附件到UIPateboard
或从中检索附件的问题。
有什么建议可以做到吗?
//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