Tapping Quicktype suggestion inserts leading whitespace

Originator:matthias.bauch
Number:rdar://19250739 Date Originated:15-Dec-2014
Status:Open Resolved:
Product:iOS SDK Product Version:8.1
Classification:Other Bug Reproducible:Always
 
Summary:
I have a UITextField which should not permit leading whitespace, so I implemented textField(textField:shouldChangeCharactersInRange:replacementString:) and return false for all edits that would lead to a content with leading whitespace. This works as expected. 

If the textField is empty and I press the space bar multiple times those are rejected and the textField stays empty. If I tap on a quicktype suggestion after doing that the inserted suggestion has multiple whitespaces in front of it. The delegate method therefor rejects the quicktype suggestion.

Steps to Reproduce:
Steps to reproduce

1. Run attached demo project
2. Enter any letter in case Quicktype is not ready yet. Delete this letter
3. Press space bar one or more times
4. Tap any quicktype suggestion (e.g.: "I'm")


Expected Results:
Quicktype inserts the suggestion into the textField


Actual Results:
Quicktype suggestion is rejected because it contains leading whitespace (e.g.: "   I'm")


Version:
iOS 8.1.2

Notes:


Configuration:
iOS Simulator, iPhone 5c

Attachments:
'QuicktypeBug.zip' was successfully uploaded.



I asked a stackoverflow question about that problem: 
http://stackoverflow.com/q/27361566/457406

The UITextFieldDelegate method:

    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 {
                // mass insert into empty field. probably from QuickType
                // so strip whitespace and set text
                let trimmedString = proposedText.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
                println("Inserted \"\(trimmedString)\" with Quicktype. ")
                textField.text = trimmedString
            }
    */
            
            return false
        }
        return true
    }

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!