使用NSTextAttachment将NSAttributed字符串存储到粘贴板和从粘贴板检索NSAttributed字符串

时间:2018-07-03 09:56:06

标签: ios nsattributedstring uipasteboard nstextattachment

我有NSAttributedStringNSTextAttachment。我已经创建了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

0 个答案:

没有答案