Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you copy text from a text view with a different font and when pasted on other apps like messages, it's the same font?

This line to just copy the text itself without font and paste it works:

pasteBoard.string = MainController.customTextView.text 

I've tried looking at similar answers that people asked about this question but almost every answer I see is in Objective C and outdated. By looking at other answers like this I think I got the copy working but when I paste the copied text, it doesn't paste anything. Here's what I have for my copy function:

    @objc func handleCopyButton() {
    MainController.customTextView.resignFirstResponder() // dismiss keyboard

    // Setup code in overridden UITextView.copy/paste
    let selectedRange = MainController.customTextView.selectedRange
    let selectedText = MainController.customTextView.attributedText.attributedSubstring(from: selectedRange)

    // UTI List
    let utf8StringType = "public.utf8-plain-text"
    let rtfdStringType = "com.apple.flat-rtfd"

    // Try custom copy
    do {
        // Convert attributedString to rtfd data
        let fullRange = NSRange(location: 0, length: selectedText.string.count)
        let data:NSData? = try selectedText.data(from: fullRange, documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtfd]) as NSData

        if let data = data {

            // Set pasteboard values (rtfd and plain text fallback)
            pasteBoard.items = [[rtfdStringType: data], [utf8StringType: selectedText.string]]

        }
    } catch { print("Couldn't copy") }

    // Copy if custom copy not available
    MainController.customTextView.copy()
}

Let me know if you have any questions on this. Thank you in advance!

Edit: For what I was trying to do, it turns out you can't copy and paste fonts (at least on iOS but in MacOS I believe you can), but what you could do is copy and paste unicode characters that looks like different fonts. Check this solution out if it's something you're interested in.

like image 803
Erik Batista Avatar asked Jul 23 '19 16:07

Erik Batista


People also ask

How do you keep font the same when pasting?

Every time. On some — but not all — apps, when you press Ctrl-V (Win)/Cmd-V (Mac), you can press Shift too. This activates “Paste and Match Style”, which converts the text to the same style as the destination document. This is usually what you want.

How do I make text a pasted font?

Keyboard shortcuts To copy the formatting of selected text: Press Ctrl + Alt + c (Windows or Chrome OS) or ⌘ + Option + c (Mac).

How do you copy different parts of text at the same time?

Copy and paste multiple items using the Office ClipboardSelect the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want.


1 Answers

  • This simply would not work on messages since apple only uses system fonts for messages.
  • In order for this to work on other apps they have to support it and have the font available to them, if on iOS the app itself has to have the font installed.
  • If you want your text to appear then you could use UIPasteboard.general.string = stringVar
  • Try some of the recommendations from this stackoverflow question.
like image 98
F22lightning Avatar answered Oct 24 '22 07:10

F22lightning