将NSAttributedString粘贴到Outside App iOS

时间:2016-08-24 17:52:29

标签: ios uipasteboard

Swift和iOS新手。我试图允许用户在我的应用程序中复制nsattributedstring并将其粘贴到Mail,iMessage或他们选择的任何应用程序中。

@IBAction func action(sender: UIButton!) {

    let stringAttributes = [
        NSFontAttributeName: UIFont.boldSystemFontOfSize(14.0),
        NSBackgroundColorAttributeName: UIColor.redColor(),
    ]
    let attributedString = NSMutableAttributedString(string: "Hello world!", attributes: stringAttributes)

    do {
        let documentAttributes = [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]
        let rtfData = try attributedString.dataFromRange(NSMakeRange(0, attributedString.length), documentAttributes: documentAttributes)
        if let rtfString = String(data: rtfData, encoding: NSUTF8StringEncoding) {
            let pb = UIPasteboard.generalPasteboard()
            return pb.string = rtfString
        }
    }
    catch {
        print("error creating RTF from Attributed String")
    }
}

粘贴后,返回:

  

Hello world!{   NSBackgroundColor =" UIDeviceRGBColorSpace 1 0 0 1&#34 ;; NSFont ="字体家庭:\" .SFUIText-Semibold \&#34 ;; font-weight:bold; font-style:normal; font-size:14.00pt&#34 ;;   }

已编辑的代码返回:

  

{\ rtf1 \ ansi \ anscipg1252 {fontal \ f0 \ fnil \ fcharset0 .SFUIText-Semibold;} {\ colortbl; \ red255 \ green255 \ blue255; \ red255 \ green0 \ blue0;} \ pard \ tx560 \ tx1120 \ tx1680 ... \ pardirnatural \ partightenfactor0 \ f0 \ b \ fs28 \ cf0 Hello world!

在做研究的过程中,我遇到了这个答案,但却无法从Leonard Pauli的回应中得到它。也许是因为那只是在应用程序中而不是粘贴到另一个应用程序中?如果这是这个问题的副本,我很抱歉。 Paste Formatted Text, Not Images or HTML

我也无法将此Copy Text with Formatting - iOS 6 NSAttributedString to Pasteboard翻译为swift

我可以粘贴纯文本而不是带有任何属性的文本。

2 个答案:

答案 0 :(得分:1)

我已更新您的代码,以便正确地将正在创建的RTF数据保存到粘贴板,但是,加载RTF数据并将其放回到NSAttributedString对象中,似乎是一项手动任务。 / p>

因此,复制和粘贴行为仅适用于开发人员明确支持以这种方式复制和粘贴RTF数据的上下文。

不幸的是,在我正在测试代码的Playgrounds中,Pasteboard的字符串属性被设置为包含RTF数据的纯文本版本(其中包含标记和奇怪的控制字符)。我无法找到解决此问题的解决方案,这意味着以这种方式不支持RTF的应用仍然可以粘贴标记的RTF明文,而不是归因于文本...... :(

这将为您提供一些方法(在应用程序中复制粘贴RTF),但显然不是很好。我的项目也依赖于这种行为,所以如果有人有其他想法,我也很想知道。

import MobileCoreServices

   // ----- Copy ------ 
func copyAttributedStringToPB() {

    let stringAttributes = [
        NSFontAttributeName: UIFont.boldSystemFontOfSize(14.0),
        NSBackgroundColorAttributeName: UIColor.redColor(),
    ]
    let attributedString = NSMutableAttributedString(string: "Hello world!", attributes: stringAttributes)

    do {
        let documentAttributes = [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]
        let rtfData = try attributedString.dataFromRange(NSMakeRange(0, attributedString.length), documentAttributes: documentAttributes)
        let pb = UIPasteboard.generalPasteboard()
        pb.setData(rtfData, forPasteboardType: kUTTypeRTF as String)
    }
    catch {
        print("error creating RTF from Attributed String")
    }
}

  // -------- Paste -------
let pb = UIPasteboard.generalPasteboard()
let data = pb.dataForPasteboardType(kUTTypeRTF as String)

let pastedAttributedString = try! NSAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType], documentAttributes: nil)

答案 1 :(得分:0)

适用于 swift 5

func copyAttributedStringToPB() {

        let stringAttributes = [
            NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14.0),
            NSAttributedString.Key.backgroundColor: UIColor.red,
        ]
        let attributedString = NSMutableAttributedString(string: "Hello world!", attributes: stringAttributes)

        do {
            let documentAttributes = [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtf]
            let rtfData = try attributedString.data(from: NSMakeRange(0, attributedString.length), documentAttributes: documentAttributes)
            let pb = UIPasteboard.general
            pb.setData(rtfData, forPasteboardType: kUTTypeRTF as String)
        }
        catch {
            print("error creating RTF from Attributed String")
        }
    }