在UIPasteBoard中复制NSAttributedString [英] Copy NSAttributedString in UIPasteBoard

查看:356
本文介绍了在UIPasteBoard中复制NSAttributedString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you copy an NSAttributedString in the pasteboard, to allow the user to paste, or to paste programmatically (with - (void)paste:(id)sender, from UIResponderStandardEditActions protocol).

我试过:

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
[pasteBoard setValue:attributedString forPasteboardType:(NSString *)kUTTypeRTF];

但这次崩溃的原因是:

-[UIPasteboard setValue:forPasteboardType:]: value is not a valid property list type'

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

which is to be expected, because NSAttributedString is not a property list value.

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

If the user paste the content of the pasteboard in my app, I would like to keep all the standards and custom attributes of the attributed string.

推荐答案

@ Guillaume使用HTML的方法对我不起作用(至少在iOS 7.1 beta 5中)。

@Guillaume's approach using HTML doesn't work for me (at least in iOS 7.1 beta 5).

更干净的解决方案是将NSAttributedStrings作为RTF(加上明文后备)插入粘贴板:

The cleaner solution is to insert NSAttributedStrings as RTF (plus plaintext fallback) into the paste board:

- (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 2.3

public extension UIPasteboard {
  public func set(attributedString: NSAttributedString?) {

    guard let attributedString = attributedString else {
      return
    }

    do {
      let rtf = try attributedString.dataFromRange(NSMakeRange(0, attributedString.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType])
      items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: NSUTF8StringEncoding)!, kUTTypeUTF8PlainText as String: attributedString.string]]

    } catch {

    }
  }
}

Swift 3

import MobileCoreServices
public extension UIPasteboard {
  public func set(attributedString: NSAttributedString?) {

    guard let attributedString = attributedString else {
      return
    }

    do {
      let rtf = try attributedString.data(from: NSMakeRange(0, attributedString.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType])
      items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: String.Encoding.utf8.rawValue)!, kUTTypeUTF8PlainText as String: attributedString.string]]

    } catch {

    }
  }
}

这篇关于在UIPasteBoard中复制NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆