获取 IOS 自定义键盘的文本字段内的当前文本

Get the current text inside of a textfield for IOS custom keyboard

我正在开发 IOS 自定义键盘。我想知道是否有一种方法可以获取文本字段中的当前文本以及它是如何工作的。

例如,我们可以使用 textDocumentProxy.hasText() 查看文本字段中是否包含文本,但我想知道文本字段中的确切字符串。

最接近的是 textDocumentProxy.documentContextBeforeInputtextDocumentProxy.documentContextAfterInput。这些将尊重句子等,这意味着如果值是一个段落,您将只会得到当前句子。众所周知,用户通过多次重新定位光标来检索整个字符串,直到检索到所有内容。

当然,如果字段需要单个值(如用户名、电子邮件、身份证号码等),您通常不必担心这一点。结合输入上下文前后的值就足够了。

示例代码

对于单个短语值,您可以:

let value = (textDocumentProxy.documentContextBeforeInput ?? "") + (textDocumentProxy.documentContextAfterInput ?? "")

对于可能包含句子结尾标点符号的值,它会稍微复杂一些,因为您需要在单独的线程中 运行 它。因此,以及您必须移动输入光标才能获得全文的事实,光标会明显移动。也不知道这是否会被 AppStore 接受(毕竟苹果可能没有故意添加一个简单的方法来获取全文,以防止官方自定义键盘侵犯用户隐私)。

注意:下面的代码基于 this Stack Overflow answer 除了针对 Swift 进行了修改,删除了不必要的睡眠,使用了没有自定义类别的字符串,并使用了更高效的移动进程.

func foo() {
    dispatch_async(dispatch_queue_create("com.example.test", DISPATCH_QUEUE_SERIAL)) { () -> Void in
        let string = self.fullDocumentContext()
    }
}

func fullDocumentContext() {
    let textDocumentProxy = self.textDocumentProxy

    var before = textDocumentProxy.documentContextBeforeInput

    var completePriorString = "";

    // Grab everything before the cursor
    while (before != nil && !before!.isEmpty) {
        completePriorString = before! + completePriorString

        let length = before!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        textDocumentProxy.adjustTextPositionByCharacterOffset(-length)
        NSThread.sleepForTimeInterval(0.01)
        before = textDocumentProxy.documentContextBeforeInput
    }

    // Move the cursor back to the original position
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(completePriorString.characters.count)
    NSThread.sleepForTimeInterval(0.01)

    var after = textDocumentProxy.documentContextAfterInput

    var completeAfterString = "";

    // Grab everything after the cursor
    while (after != nil && !after!.isEmpty) {
        completeAfterString += after!

        let length = after!.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)

        textDocumentProxy.adjustTextPositionByCharacterOffset(length)
        NSThread.sleepForTimeInterval(0.01)
        after = textDocumentProxy.documentContextAfterInput
    }

    // Go back to the original cursor position
    self.textDocumentProxy.adjustTextPositionByCharacterOffset(-(completeAfterString.characters.count))

    let completeString = completePriorString + completeAfterString

    print(completeString)

    return completeString
}