QuickType predictions take key strokes into account that should be blocked by my UITextFieldDelegate

我的梦境 提交于 2019-12-05 14:27:33

With some more testing I came to the conclusion that this is a bug.

First I thought that Keyboard and QuickType are decoupled from the UITextField. But this is actually not the case. Changing the position of the cursor in a filled textField actually changes quicktype suggestions. So the textField actually communicates with quicktype.

So I filed a bug.

Bug at Apple: rdar://19250739
Bug at OpenRadar: 5794406481264640

In case somebody is interested in the workaround:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!