首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制NSAttributedString中的UIPasteBoard

复制NSAttributedString中的UIPasteBoard
EN

Stack Overflow用户
提问于 2012-09-26 11:46:51
回答 4查看 7.3K关注 0票数 11

如何在中复制NSAttributedString,以允许用户粘贴,或以编程方式粘贴(使用- (void)paste:(id)sender,从UIResponderStandardEditActions协议)。

我试过:

代码语言:javascript
复制
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
[pasteBoard setValue:attributedString forPasteboardType:(NSString *)kUTTypeRTF];

但这次坠机事件有:

代码语言:javascript
复制
-[UIPasteboard setValue:forPasteboardType:]: value is not a valid property list type'

这是预期的,因为NSAttributedString不是一个属性列表值。

如果用户在我的应用程序中粘贴了贴图板的内容,我希望保留属性字符串的所有标准和自定义属性。

EN

回答 4

Stack Overflow用户

发布于 2012-09-26 13:45:30

我发现,当我(作为应用程序的用户)将富文本从UITextView复制到pasteboard中时,pasteboard包含两种类型:

代码语言:javascript
复制
"public.text",
"Apple Web Archive pasteboard type

基于此,我在UIPasteboard上创建了一个方便的类别。

(大量使用来自this answer的代码)。

它很有效,但:

转换为html格式意味着我将失去自定义属性。任何清洁的解决方案都将被欣然接受。

文件UIPasteboard+AttributedString.h:

代码语言:javascript
复制
@interface UIPasteboard (AttributedString)

- (void) setAttributedString:(NSAttributedString *)attributedString;

@end

文件UIPasteboard+AttributedString.m:

代码语言:javascript
复制
#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上,请访问我的客人:)

票数 10
EN

Stack Overflow用户

发布于 2014-02-20 15:19:48

清洁的解决方案是将NSAttributedString作为RTF (加上明文回退)插入粘贴板中,而不是涉及HTML:

代码语言:javascript
复制
- (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

代码语言:javascript
复制
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 {
        }
    }
}
票数 10
EN

Stack Overflow用户

发布于 2016-11-17 06:05:51

这很简单:

代码语言:javascript
复制
  #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];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12601039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档