Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a UITextField to lowercase while typing and retaining cursor position

Tags:

ios

xcode7

swift2

(programming in swift 2)

I have a UITextField that when the user types into it should be automatically converted to lower case WHILE typing (so NOT after form validation).

I have gotten this far:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    //only convert to lowercase for username field
    if textField == textFieldUsername {
        //let stringRange = NSRange(location: range.location, length: range.length)
        let completedString = (textField.text ?? "" as NSString).stringByReplacingCharactersInRange(range, withString: string)
        //convert the while thing to lowercase and assign back to the textfield
        textField.text = completedString.lowercaseString
        //return false to indicate that the "system" itself should not do anychanges anymore, as we did them
        return false
    }
    //return to the "system" that it can do the changes itself
    return true
}

Problem is that (1)when the user presses and holds on the UITextField to (2)move the cursor to somewhere halfway inside the string and (3)start typing that (4)the cursor jumps back to the end of the already inputted string.

Does the cursor position needs to be restored after textField: shouldChangeCharactersInRange is called maybe?

like image 966
HixField Avatar asked Dec 25 '22 08:12

HixField


2 Answers

I have taken your code and tried this.

I can replace the cursor position wherever the text is changed.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let start = textField.positionFromPosition(textField.beginningOfDocument, offset:range.location)

    let cursorOffset = textField.offsetFromPosition(textField.beginningOfDocument, toPosition:start!) + string.characters.count


    textField.text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string).lowercaseString

    let newCursorPosition = textField.positionFromPosition(textField.beginningOfDocument, offset:cursorOffset)

    let newSelectedRange = textField.textRangeFromPosition(newCursorPosition!, toPosition:newCursorPosition!)

    textField.selectedTextRange = newSelectedRange

    return false
}
like image 99
UIResponder Avatar answered Jan 05 '23 23:01

UIResponder


Swift 4

lowercaseString has changed to lowercased(). Also you can do this via target action instead of NotificationCenter if you prefer.

@IBOutlet fileprivate weak var textField: UITextField!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}

@objc func textFieldDidChange(textField: UITextField) {
    textField.text = textField.text?.lowercased()
} 
like image 28
Eli Burke Avatar answered Jan 05 '23 21:01

Eli Burke