When I'm trying to copy a text from the FMP and paste it to any textfield of my app, I'm getting asian symbols instead of copied text.
Here is an example:
Text in the FMP: GordonValley2 (I pasted it here directly from the FMP)
Text in my app after paste into a SearchBar: 䜀漀爀搀漀渀嘀愀氀氀攀礀㈀
I don't have any subclasses of a SearchBar.
This is a code example of adding the SearchBar:
searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchBar.searchBarStyle = .minimal
searchBar.isTranslucent = false
searchBar.showsCancelButton = true
searchBar.delegate = searchTableController
searchBarView.addSubview(searchBar)
It happens both when I'm launching a MacOS app, or an iOS app in the simulator.
I tried to check the text from the pasteboard in different methods of a custom textfield class like shouldChangeCharactersIn of UITextFieldDelegate, or in textPasteConfigurationSupporting of UITextPasteDelegate, but I'm always getting something like this:
Optional - some : 䜀漀爀搀漀渀嘀愀氀氀攀礀㈀{ NSColor = "UIExtendedSRGBColorSpace 0.373 0.447 1 1"; NSFont = "\"SFProText-Medium 15.00 pt. P [] (0x1014e55b0) fobj=0x1014e55b0, spc=4.10\""; NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 4, Tabs (\n 28L,\n 56L,\n 84L,\n 112L,\n 140L,\n 168L,\n 196L,\n 224L,\n 252L,\n 280L,\n 308L,\n 336L\n), DefaultTabInterval 0, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0 LineBreakStrategy 1"; NSShadow = "NSShadow {0, -1} color = {(null)}";
Pasting the same text into the SearchBar or any Text Field of any other apps like Slack, Apple Music, Browser works well.
Also, Copy/Paste works well from browser, pdf files with different languages and rich text, so I'm not sure how to track the issue
I was able to fix this issue by using UITextPasteDelegate
Here is an example:
func textPasteConfigurationSupporting(_ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting, combineItemAttributedStrings itemStrings: [NSAttributedString], for textRange: UITextRange) -> NSAttributedString {
if itemStrings.count == 1, let first = itemStrings.first {
///remove all attributes
let pasteBoard = UIPasteboard.general
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
.documentType: NSAttributedString.DocumentType.rtf
]
if
let data = pasteBoard.data(forPasteboardType: "public.rtf"),
let attString = try? NSAttributedString(data: data,
options: options, documentAttributes: nil){
return attString
}
return NSAttributedString(string: first.string)
}
return NSAttributedString()
}
This code, however, will remove all font attributes from the string, so it will be better to apply them back for a clear string
EDIT: This code works fine, I made an improvements and moved the code to the textFieldSHouldChangeCharacters... method and removed the pasteConfiguration delegate.
But for some reason, this line:
let data = pasteBoard.data(forPasteboardType: "public.rtf")
Causes crash on MacOS