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

I have a textField in which I do not want to allow leading spaces. So I implemented textField(textField:shouldChangeCharactersInRange:replacementString:)

and blocked attempts that would change the text to something starting with spaces. This works as expected.

Unfortunately, this will mess up QuickType. Every time I hit a space in a blank field, which is then ignored by my text field, the Quicktype text gets prefixed with that space. To make it clearer, the text that QuickType will insert is prefixed, no extra spaces appear in the QuickType panel 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
}

      

Here are some logs to see what happens when I press spacebar 5 times and then click on the I'm

QuickType suggestion :

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

      

By checking the variables in this delegate method, I can check if the problem is really with the string replacement that I am getting from the UITextField. It already contains a sentence prefixed with spaces.

Does anyone know how I can prevent this, or how can I suggest a "reset" QuickType?

A workaround would be to trim whitespace from wordy inserts, but first I want to see if there is not enough way to solve the problem in a clean way.

+3


source to share


1 answer


On further testing, I concluded that this is a bug.

At first I thought the Keyboard and QuickType were separate from the UITextField. But actually it is not. Changing the position of the cursor in a filled text box actually changes the quick-type sentences. This way the textField does bind to the quicktype.

So I filed the error.



Error in Apple: rdar: // 19250739
Error in OpenRadar: 5794406481264640

If anyone is interested in a 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
}

      

0


source







All Articles