how to move cursor in UITextField after setting its value how to move cursor in UITextField after setting its value ios ios

how to move cursor in UITextField after setting its value

After the UITextField.text property is changed, any previous references to UITextPosition or UITextRange objects that were associated with the old text will be set to nil after you set the text property. You need to store what the text offset will be after the manipulation will be BEFORE you set the text property.

This worked for me (note, you do have to test whether cursorOffset is < textField.text.length if you remove any characters from t in the example below):

- (BOOL) textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange) range replacementString:(NSString *) string{    UITextPosition *beginning = textField.beginningOfDocument;    UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];    UITextPosition *end = [textField positionFromPosition:start offset:range.length];    UITextRange *textRange = [textField textRangeFromPosition:start toPosition:end];    // this will be the new cursor location after insert/paste/typing    NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length;     // now apply the text changes that were typed or pasted in to the text field    [textField replaceRange:textRange withText:string];    // now go modify the text in interesting ways doing our post processing of what was typed...    NSMutableString *t = [textField.text mutableCopy];    t = [t upperCaseString];    // ... etc    // now update the text field and reposition the cursor afterwards    textField.text = t;    UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];    UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];    [textField setSelectedTextRange:newSelectedRange];    return NO;}

And here is the swift version :

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {    let beginning = textField.beginningOfDocument    let start = textField.positionFromPosition(beginning, offset:range.location)    let end = textField.positionFromPosition(start!, offset:range.length)    let textRange = textField.textRangeFromPosition(start!, toPosition:end!)    let cursorOffset = textField.offsetFromPosition(beginning, toPosition:start!) + string.characters.count// just used same text, use whatever you want :)    textField.text = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)    let newCursorPosition = textField.positionFromPosition(textField.beginningOfDocument, offset:cursorOffset)    let newSelectedRange = textField.textRangeFromPosition(newCursorPosition!, toPosition:newCursorPosition!)    textField.selectedTextRange = newSelectedRange    return false}

Here is a swift 3 version

extension UITextField {    func setCursor(position: Int) {        let position = self.position(from: beginningOfDocument, offset: position)!        selectedTextRange = textRange(from: position, to: position)    }}