ios - 在 Swift 3 中解析 UITextDocumentProxy

标签 ios swift

我目前正在开发一个自定义键盘应用程序,但无法解析键盘输出到文本文档代理的内容。如何解决这个问题呢?我感觉我快要失去理智了。目前我正在循环:

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/

相关文章:

arrays - 在 UITableView iOS Swift 中循环遍历数组并使用自定义单元格设置值时出现问题

ios - swift iOS : How can I get player's name in SpriteKit?

ios - 从 'FIRRemoteConfigValue!' 转换为无关类型 'String' 总是失败

ios - 什么是 Objective-C 等同于空的 Swift OptionSet?

ios - MKMapView addAnnotation 不起作用

ios - 如何在 iOS 中创建和检索新的位图

swift - 水平滚动 UICollectionView 有额外的行间距?

ios - 快速将数据推送到 UISplitViewController 的主视图 Controller 和详细 View Controller

ios - Prepare for segue 是自动调用的

ios - 只向右滚动的 UICollectionView(没有向左、向上或向下滚动)