Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move cursor in UITextField after setting its value

can anybody help me with this: i need to implement UITextField for input number. This number should always be in decimal format with 4 places e.g. 12.3456 or 12.3400. So I created NSNumberFormatter that helps me with decimal places.

I am setting the UITextField value in

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method. I proccess input, use formatter and finally call [textField setText: myFormattedValue];

This works fine but this call also moves the cursor to the end of my field. That is unwanted. E.g. I have 12.3400 in my field and the cursor is located on the very beginning and user types number 1. The result value is 112.3400 but cursor is moved at the end. I want to end with cursor when the user expects (just after the number 1 recently added). There are some topics about setting cursor in TextView but this is UITextField. I also tried to catch selectedTextRange of the field, which saves the cursor position properly but after setText method call, this automatically changes and the origin UITextRange is lost (changed to current). hopefully my explanation is clear.

Please, help me with this. thank you very much.

EDIT : Finally, i decided to switch the functionality to changing the format after whole editing and works good enough. I have done it by adding a selector forControlEvents:UIControlEventEditingDidEnd.

like image 481
user1135839 Avatar asked Jun 22 '12 14:06

user1135839


People also ask

What is Uitextfield?

An object that displays an editable text area in your interface.

How do you delete Uitextfield?

The standard clear button is displayed at the right side of the text field as a way for the user to remove text quickly. This button appears automatically based on the value set for this property.


2 Answers

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; } 
like image 65
JasonD Avatar answered Sep 25 '22 02:09

JasonD


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 } 
like image 22
ergunkocak Avatar answered Sep 24 '22 02:09

ergunkocak