I'm copying an NSAttributedString with text and a custom font to UIPasteboard as RTF, with the following code:
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)
}
When I print the string representation of the UIPasteboard, I get the following:
Optional("{\rtf1\ansi\ansicpg1252\n{\fonttbl\f0\fnil\fcharset0 Copperplate-Bold;}\n{\colortbl;\red255\green255\blue255;}\n{\*\expandedcolortbl;;}\n\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0\n\n\f0\b\fs30 \cf0 hello}")
However, when I manually paste the code in a separate app, it pastes nothing. I can't seem to figure out how to copy formatted text in Swift 4, so it can be pasted in a separate application by manually pasting it. Any help would be greatly appreciated.
Thanks!