将粘贴NSAttribute字符串复制到外部应用程序iOS

将粘贴NSAttribute字符串复制到外部应用程序iOS,ios,uipasteboard,Ios,Uipasteboard,Swift和iOS的新功能。我正在尝试允许用户在我的应用程序中复制nsattributedstring并将其粘贴到Mail、iMessage或他们选择的任何应用程序中 @IBAction func action(sender: UIButton!) { let stringAttributes = [ NSFontAttributeName: UIFont.boldSystemFontOfSize(14.0), NSBackgroundColorAttr

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")
    }
}
粘贴后,将返回:

你好,世界!{ NSBackgroundColor=“UIDeviceRGBColorSpace 1 0 0 1”;NSFont=“font-family:\”.SFUIText-Semibold\“字体大小:粗体;字体样式:普通;字体大小:14.00pt”; }

编辑代码返回:

{\rtf1\ansi\anscipg1252{fontal\f0\fnil\fcharset0.SFUIText Semibold;}{\colortbl;\red255\green255\blue255;\red255\green0\blue0;}\pard\tx560\tx1120\tx1680…\pardirnatural\partitionfactor0\f0\f0\b\fs28\cf0你好,世界

在做研究时,我偶然发现了这个答案,但从Leonard Pauli的回答中无法得到它。可能是因为这只是在应用程序中,而不是粘贴到另一个应用程序中?如果这是这个问题的重复,我很抱歉。

我也无法将此翻译成swift


我可以粘贴纯文本,但不能粘贴具有任何属性的文本。

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

因此,复制和粘贴行为仅在开发人员明确支持以这种方式复制和粘贴RTF数据的上下文中有效

不幸的是,在我测试代码的游乐场中,粘贴板的string属性被设置为包含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)

适用于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")
        }
    }

现在,您正在将NSAttributed string对象的字符串描述复制到粘贴板上。虽然这是Objective-C中的一篇文章,但可能有一个适合您的策略。谢谢您的回复。您会使用Guillaume还是Ortwin Gentz answer?我以前遇到过这个问题,尝试Guillaume的答案时运气不佳。@cpimhoff尚未测试我自己也不同意,但Ortwin的答案对我来说似乎更实际。关于update@cpimhoff的任何想法您编辑的代码正确地创建了RTF数据(据我所知),但现在您正在将RTF数据对象的字符串描述保存到粘贴板。与前面的问题大致相同。您需要使用“将RTF数据正确地添加到粘贴板”(作为键为数据类型的字典)。我将更新您的代码,并将很快发布答案。这会导致错误“上下文类型'AnyObject'不能与字典文字一起使用”在pb.items行。处理解决方案@cpimhofforry。我离开编译器有一段时间了……根据您的Swift版本,您可能还需要将
kUTTypeRYF
kUTTypeUTF8PlainText
转换为Swift字符串。感谢您的响应。强制转换为字符串返回的内容与上面编辑的代码相同伊利。在操场上玩了很多次之后,我找到了上面的解决方案——这并不能特别解决您的问题(我深表歉意)。开发人员的明确支持一直是我的难题,但我感谢您的努力。我最近发现了一个应用程序(自定义键盘)名为“彩色文本”的功能似乎与我的问题类似。我认为开发人员的方法可能会使用图像,但我无法复制它。