I have a textField, in which I don't want to allow leading whitespace. So I implemented textField(textField:shouldChangeCharactersInRange:replacementString:) and block attempts which would change the text to something that starts with whitespace. This works as expected.
Unfortunately this messes up QuickType. Each time I press space in an empty field, which is then ignored by my textField, the Quicktype text gets prefixed with this space. To be more clear, the text that QuickType will insert get's prefixed, the additional whitespace is not shown in the UI of the QuickType bar.
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 is some logging to see what's happening when I press space 5 times followed by press 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 inspecting the variables in that delegate method I can verify that the problem is indeed with the replacement string, that I get from the UITextField. It already contains the suggestion prefixed with whitespace.
Does anybody know how I can prevent that, or how I can "reset" QuickType suggestions?
A workaround would be to trim whitespace from multi character inserts, but first I want to see if I'm missing a way to handle the problem in a clean way.
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
}
来源:https://stackoverflow.com/questions/27361566/quicktype-predictions-take-key-strokes-into-account-that-should-be-blocked-by-my