【问题标题】:Get the current text inside of a textfield for IOS custom keyboard获取 IOS 自定义键盘的文本字段内的当前文本
【发布时间】:2016-06-21 21:56:46
【问题描述】:

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

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

【问题讨论】:

    标签: ios iphone-softkeyboard


    【解决方案1】:

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

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

    示例代码

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

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

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

    注意:以下代码基于 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
    }
    

    【讨论】:

    • 能否给出一些示例代码来说明这是如何完成的?
    • 我为这两种情况添加了示例代码。请注意,全文方式可能是 Apple 的疏忽,因为我无法想象他们会希望键盘检索全文。但话说回来,谁知道呢。
    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多