如何在中复制NSAttributedString,以允许用户粘贴,或以编程方式粘贴(使用- (void)paste:(id)sender,从UIResponderStandardEditActions协议)。
我试过:
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
[pasteBoard setValue:attributedString forPasteboardType:(NSString *)kUTTypeRTF];但这次坠机事件有:
-[UIPasteboard setValue:forPasteboardType:]: value is not a valid property list type'这是预期的,因为NSAttributedString不是一个属性列表值。
如果用户在我的应用程序中粘贴了贴图板的内容,我希望保留属性字符串的所有标准和自定义属性。
发布于 2012-09-26 13:45:30
我发现,当我(作为应用程序的用户)将富文本从UITextView复制到pasteboard中时,pasteboard包含两种类型:
"public.text",
"Apple Web Archive pasteboard type基于此,我在UIPasteboard上创建了一个方便的类别。
(大量使用来自this answer的代码)。
它很有效,但:
转换为html格式意味着我将失去自定义属性。任何清洁的解决方案都将被欣然接受。。
文件UIPasteboard+AttributedString.h:
@interface UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString;
@end文件UIPasteboard+AttributedString.m:
#import <MobileCoreServices/UTCoreTypes.h>
#import "UIPasteboard+AttributedString.h"
@implementation UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString {
NSString *htmlString = [attributedString htmlString]; // This uses DTCoreText category NSAttributedString+HTML - https://github.com/Cocoanetics/DTCoreText
NSDictionary *resourceDictionary = @{ @"WebResourceData" : [htmlString dataUsingEncoding:NSUTF8StringEncoding],
@"WebResourceFrameName": @"",
@"WebResourceMIMEType" : @"text/html",
@"WebResourceTextEncodingName" : @"UTF-8",
@"WebResourceURL" : @"about:blank" };
NSDictionary *htmlItem = @{ (NSString *)kUTTypeText : [attributedString string],
@"Apple Web Archive pasteboard type" : @{ @"WebMainResource" : resourceDictionary } };
[self setItems:@[ htmlItem ]];
}
@end只实现了setter。如果您想编写getter,并/或将其放在GitHub上,请访问我的客人:)
发布于 2014-02-20 15:19:48
清洁的解决方案是将NSAttributedString作为RTF (加上明文回退)插入粘贴板中,而不是涉及HTML:
- (void)setAttributedString:(NSAttributedString *)attributedString {
NSData *rtf = [attributedString dataFromRange:NSMakeRange(0, attributedString.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType}
error:nil];
self.items = @[@{(id)kUTTypeRTF: [[NSString alloc] initWithData:rtf encoding:NSUTF8StringEncoding],
(id)kUTTypeUTF8PlainText: attributedString.string}];
}Swift 5
import MobileCoreServices
public extension UIPasteboard {
func set(attributedString: NSAttributedString) {
do {
let rtf = try attributedString.data(from: NSMakeRange(0, attributedString.length), documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtf])
items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: String.Encoding.utf8.rawValue)!, kUTTypeUTF8PlainText as String: attributedString.string]]
} catch {
}
}
}发布于 2016-11-17 06:05:51
这很简单:
#import <MobileCoreServices/UTCoreTypes.h>
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
NSData *rtf = [attributedString dataFromRange:NSMakeRange(0, attributedString.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
error:nil];
if (rtf) {
[item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
}
[item setObject:attributedString.string forKey:(id)kUTTypeUTF8PlainText];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.items = @[item];https://stackoverflow.com/questions/12601039
复制相似问题