我将带有文本和自定义字体的NSAttributedString作为RTF复制到UIPasteboard,并使用以下代码:
let attrString2 = NSAttributedString(string: myString!, attributes: [NSAttributedStringKey.font: UIFont.init(name: listOfFonts[(indexPath?.row)!], size: 15)])
do {
let rtf = try attrString2.data(from: NSMakeRange(0, attrString2.length), documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.rtf])
UIPasteboard.general.setData(rtf, forPasteboardType: kUTTypeRTF as String)
print(UIPasteboard.general.string)
}
当我打印UIPasteboard的字符串表示时,我得到以下内容:
可选(" {\ rtf1 \ ansi \ ansicpg1252 \ n {\ fonttbl \ f0 \ fnil \ fcharset0 Copperplate-Bold;} \ n {\ colortbl; \ red255 \ green255 \ blue255;} \ n \ n \ * \ expandedcolortbl ;;} \ n \ pard \ tx560 \ tx1120 \ tx1680 \ tx2240 \ tx2800 \ tx3360 \ tx3920 \ tx4480 \ tx5040 \ tx5600 \ tx6160 \ tx6720 \ pardirnatural \ partightenfactor0 \ n \ n \ f0 \ b \ fs30 \ cf0你好}&#34)
但是,当我手动将代码粘贴到单独的应用程序中时,它不会粘贴任何内容。我似乎无法弄清楚如何在Swift 4中复制格式化文本,因此可以通过手动粘贴将其粘贴到单独的应用程序中。任何帮助将不胜感激。
谢谢!