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.
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.
Keyboard shortcuts To copy the formatting of selected text: Press Ctrl + Alt + c (Windows or Chrome OS) or ⌘ + Option + c (Mac).
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.
UIPasteboard.general.string = stringVar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With