swift - 从 FileMaker 复制/粘贴后出现奇数符号
问题描述
当我尝试从 FMP 复制文本并将其粘贴到我的应用程序的任何文本字段时,我得到的是亚洲符号而不是复制的文本。
这是一个例子:
FMP 中的文本:GordonValley2(我直接从 FMP 粘贴到这里)
粘贴到搜索栏后在我的应用程序中的文本:䜀漀爀搀漀渀嘀愀氀氀攀礀㈀
我没有 SearchBar 的任何子类。
这是添加 SearchBar 的代码示例:
searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search..."
searchBar.searchBarStyle = .minimal
searchBar.isTranslucent = false
searchBar.showsCancelButton = true
searchBar.delegate = searchTableController
searchBarView.addSubview(searchBar)
当我在模拟器中启动 MacOS 应用程序或 iOS 应用程序时,都会发生这种情况。
我试图在自定义文本字段类的不同方法中检查粘贴板中的文本,例如 UITextFieldDelegate 的 shouldChangeCharactersIn 或 UITextPasteDelegate 的 textPasteConfigurationSupporting,但我总是得到这样的结果:
可选 - 一些:䜀漀爀搀漀渀嘀愀氀氀攀礀㈀{ 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, 块 (null), 列表 (null) , BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0 LineBreakStrategy 1"; NSShadow = "NSShadow {0, -1} 颜色 = {(null)}";
将相同的文本粘贴到 SearchBar 或任何其他应用程序(如 Slack、Apple Music、浏览器)的任何文本字段中效果很好。
此外,复制/粘贴在浏览器、具有不同语言和富文本的 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 委托。
但由于某种原因,这一行:
让数据 = pasteBoard.data(forPasteboardType: "public.rtf")
导致 MacOS 崩溃
推荐阅读
- c++ - 如何删除在类旁边用“new”声明的类中的对象
- sql - 自动生成唯一密钥不一致
- reactjs - 如何在移动设备上启用 CanvasJS 图表中的滚动?
- ios - UITableView 单元格不显示所有数据
- sql - 引用第二个表的新列 - 我需要加入吗?
- mysql - 其中包含 STR_TO_DATE 和 SUBSTRING
- python - 获取 jupyter notebook 以打印它已导入的整个函数
- javascript - React.js 表单 - 如何在最后一页包含所有值?
- mysql - 执行此 INSERT 语句时,我应该更改什么以避免 Aurora MySQL 错误 1034?
- python - 无法读取正确的 buildozer logcat