cocoa-touch - QuickType 预测会考虑应该被我的 UITextFieldDelegate 阻止的击键

标签 cocoa-touch swift uitextfield quicktype

我有一个文本字段,我不想在其中使用前导空格。所以我实现了 textField(textField:shouldChangeCharactersInRange:replacementString:) 并阻止将文本更改为以空格开头的内容的尝试。这按预期工作。

不幸的是,这会弄乱 QuickType。每次我在一个空字段中按空格键,然后我的 textField 会忽略它,Quicktype 文本就会以这个空格为前缀。更清楚地说,QuickType 将插入的文本带有前缀,额外的空格不会显示在 QuickType 栏的 UI 中。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text as NSString
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string)
    if !proposedText.isEmpty {
        let firstCharacterString = String(proposedText[proposedText.startIndex]) as NSString
        if firstCharacterString.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).location == 0 {
            println("starts with whitespace \"\(proposedText)\"")
            return false
        }
    }
    return true
}

这里是一些日志记录,以查看当我按空格键 5 次,然后按 I'm QuickType 建议时发生了什么:

starts with whitespace " "           // pressing space key
starts with whitespace " "
starts with whitespace " "
starts with whitespace " "
starts with whitespace " "
starts with whitespace "     I'm"    // Inserted by QuickType after pressing the "I'm" suggestion
starts with whitespace " "           // Inserted by QuickType as well

通过检查该委托(delegate)方法中的变量,我可以验证问题确实出在我从 UITextField 获得的替换字符串上。它已经包含以空格为前缀的建议。

有谁知道我该如何防止这种情况,或者我如何“重置”QuickType 建议?

解决方法是从多字符插入中删除空格,但首先我想看看我是否缺少一种以干净的方式处理问题的方法。

最佳答案

经过更多测试,我得出结论,这是一个错误。

首先,我认为 Keyboard 和 QuickType 与 UITextField 是分离的。但实际上并非如此。在填充的文本字段中更改光标的位置实际上会更改快速输入建议。所以 textField 实际上与 quicktype 通信。

所以我提交了一个错误。

Apple 漏洞:rdar://19250739
OpenRadar 的错误:5794406481264640

如果有人对解决方法感兴趣:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let currentText = textField.text as NSString
    let proposedText = currentText.stringByReplacingCharactersInRange(range, withString: string)

    if proposedText.hasPrefix(" ") {
        // Don't allow space at beginning
        println("Ignore text \"\(proposedText)\"")

        // workaround
        if textField.text == "" && countElements(string) > 1 {
            // multi insert into empty field. probably from QuickType
            // so we should strip whitespace and set new text directly
            let trimmedString = proposedText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
            println("Inserted \"\(trimmedString)\" with Quicktype. ")
            textField.text = trimmedString
        }

        return false
    }
    return true
}

关于cocoa-touch - QuickType 预测会考虑应该被我的 UITextFieldDelegate 阻止的击键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361566/

相关文章:

iphone - cellForRowAtIndexPath 中的崩溃

ios - 如何将委托(delegate)函数转换为 IBAction 函数

iphone - 如何以位显示 NSData 的内容?

ios - 使用解密时,表达式类型不明确,没有更多 AES 上下文

swift - If Else 语句与选择器的链接

ios - UITextField 字符限制

cocoa-touch - 绘制圆并存储在 UIImage 中

保持水平的 Swift SpriteKit 马里奥风格旋转平台

iphone - UIAlertView 选择导致 resignFirstResponder 失败

ios - 快速处理文本字段中的测试更改事件