Format UITextField text without having cursor move to the end

Bernd picture Bernd · Oct 9, 2014 · Viewed 14.9k times · Source

I am trying to apply NSAttributedString styles to a UITextField after processing a new text entry, keystroke by keystroke. The problem is that any time I replace the text the cursor will jump the very end on each change. Here's my current approach …

Receive and display text change immediately (same issue applies when I return false and do the text replacement manually)

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {        
    let range:NSRange = NSRange(location: range.location, length: range.length)
    let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string);
    return true
}

I subscribed to the UITextFieldTextDidChangeNotification notification. This triggers the styling. When the text is changed I replace it (NSString) with the formatted version (NSAttributedString) using some format() function.

func textFieldTextDidChangeNotification(userInfo:NSDictionary) {
    var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString)
    attributedString = format(textField.text) as NSMutableAttributedString
    textField.attributedText = attributedString
}

Styling works fine like this. However after each text replacement the cursor jumps to the end of the string. How can I turn off this behaviour or manually move the cursor back where it started the edit? … or is there a better solution to style the text while editing?

Answer

Stonz2 picture Stonz2 · Oct 9, 2014

The way to make this work is to grab the location of the cursor, update the field contents, and then replace the cursor to its original position. I'm not sure of the exact equivalent in Swift, but the following is how I would do it in Obj-C.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    UITextPosition *beginning = textField.beginningOfDocument;
    UITextPosition *cursorLocation = [textField positionFromPosition:beginning offset:(range.location + string.length)];

    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    /* MAKE YOUR CHANGES TO THE FIELD CONTENTS AS NEEDED HERE */

    // cursorLocation will be (null) if you're inputting text at the end of the string
    // if already at the end, no need to change location as it will default to end anyway
    if(cursorLocation)
    {
        // set start/end location to same spot so that nothing is highlighted
        [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation toPosition:cursorLocation]];
    }

    return NO;
}