我目前正在开发一个自定义键盘应用程序,但无法解析键盘输出到文本文档代理的内容。如何解决这个问题呢?我感觉我快要失去理智了。目前我正在循环:
for letter in (proxy.documentContextBeforeInput?.characters)!
但是,这只是获取光标当前所在行上光标之前的文本,这样如果我的 textDocumentProxy 包含:
Some text above
Some text below (cursor position)
我的循环仅迭代“下面的一些文本”部分。
有没有办法循环遍历整个 UITextDocumentProxy?谢谢。
最佳答案
documentContextBeforeInput
,顾名思义,只返回输入之前的文本。要获取完整的文本字符串,您应该执行以下操作:
let entireText = (proxy.documentContextBeforeInput ?? "") + (proxy.documentContextAfterInput ?? "")
if let chars = entireText.characters {
for letter in chars {
//DO something useful
}
}
关于ios - 在 Swift 3 中解析 UITextDocumentProxy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41127673/