Swift 从FileMaker复制/粘贴后出现奇数符号
当我试图从FMP复制文本并将其粘贴到我应用程序的任何文本字段时,我得到的是亚洲符号,而不是复制的文本 以下是一个例子:Swift 从FileMaker复制/粘贴后出现奇数符号,swift,mac-catalyst,Swift,Mac Catalyst,当我试图从FMP复制文本并将其粘贴到我应用程序的任何文本字段时,我得到的是亚洲符号,而不是复制的文本 以下是一个例子: func textPasteConfigurationSupporting(_ textPasteConfigurationSupporting: UITextPasteConfigurationSupporting, combineItemAttributedStrings itemStrings: [NSAttributedString], for textRange: U
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()
}
FMP中的文本:GordonValley2(我直接从FMP粘贴到这里)
粘贴到搜索栏后我的应用程序中的文本:䜀漀爀搀漀渀嘀愀氀氀攀礀㈀
我没有搜索栏的任何子类
这是添加搜索栏的代码示例:
searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchBar.searchBarStyle = .minimal
searchBar.isTranslucent = false
searchBar.showsCancelButton = true
searchBar.delegate = searchTableController
searchBarView.addSubview(searchBar)
当我在模拟器中启动MacOS应用程序或iOS应用程序时,都会发生这种情况
我试图用自定义textfield类的不同方法检查粘贴板中的文本,如UITextFieldDelegate的shouldChangeCharactersIn,或UITextPasteDelegate的textPasteConfigurationSupporting,但我总是得到这样的结果:
可选的
-一些:䜀漀爀搀漀渀嘀愀氀氀攀礀㈀{
NSColor=“UIExtendedSRGBColorSpace 0.373 0.447 1”;
NSFont=“\”SFProText介质15.00 pt.P[](0x1014e55b0)fobj=0x1014e55b0,spc=4.10\”;
NSParagraphStyle=“对齐4、行距0、段落间距0、段落间距0、段落间距0、首缩进0、尾缩进0、首行首缩进0、线宽0/0、线宽倍数0、换行模式4、制表符(\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),List(null),BaseWritingDirection 0,连字符系数0,TighteningForRunning是,HeaderLevel 0 LineBreakStrategy 1”;
NSShadow=“NSShadow{0,-1}color={(null)}”
将相同的文本粘贴到搜索栏或任何其他应用程序(如Slack、Apple Music、Browser)的任何文本字段中效果良好
另外,从浏览器、使用不同语言和富文本的pdf文件中复制/粘贴也能很好地工作,因此我不确定如何跟踪此问题我能够通过使用
UITextPasteDelegate解决此问题
以下是一个例子:
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()
}
但是,这段代码将从字符串中删除所有字体属性,因此最好将它们重新应用为清晰的字符串
编辑:
这段代码运行良好,我做了一些改进,将代码移动到textFieldSHouldChangeCharacters…方法,并删除了pasteConfiguration委托
但出于某种原因,这一行:
让数据=粘贴板.data(用于粘贴板类型:“public.rtf”)
导致MacOS崩溃