0

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!

  • The other app may not support RTF. Try a format supported by the other app. – rmaddy May 21 '18 at 22:46
  • @rmaddy thanks for the reply. The app does support RTF, because if I copy formatted text from Safari, it is able to be pasted in the app. – Jacob Perks May 22 '18 at 23:27
  • But formatted text from Safari isn't RTF. It's web content. – rmaddy May 22 '18 at 23:33
  • @rmaddy I read somewhere that it copies it as RTF, thats very helpful thanks! – Jacob Perks May 24 '18 at 01:16
  • Add a little code to your app to display the `types` from `UIPasteboard`. Then copy some text in Safari, bring up your app and see what those `types` are. Once you see those types and figure which ones support full formatting, update the code in this question to use the appropriate type. – rmaddy May 24 '18 at 02:01

0 Answers0