首页 > 解决方案 > 从 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 文件中效果很好,所以我不确定如何跟踪问题

标签: swiftmac-catalyst

解决方案


我能够通过使用解决此问题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 崩溃


推荐阅读