Reputation: 836
The user should not be able to move the cursor of a UITextField somwhere else in the entered string.
I need this because I want the user to enter a currency amount. This is done by letting the user enter numbers which will be added to the end of the amount and the commata will be moved to the third position from the end.
How do I force the cursor in an UITextField to stay at the end of an entered string?
Upvotes: 2
Views: 2025
Reputation: 9834
Place a UIButton
over your UITextField
. Add an IBAction
to the button and have it call UITextField.becomeFirstResponder()
.
This prevents clicks from passing, cursors for being able to be modified, but still allows the user to click the view and select it.
Upvotes: 2
Reputation: 61
Create a subclass of UITextField with
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return NO; }
to disable all gestures on that field. This implies that you make that field the firstResponder programmatically though.
Upvotes: 1
Reputation: 836
I solved the problem by hiding the original entry field and show the value formatted in a label. This prevents the user from changing the cursors position.
Upvotes: 1
Reputation: 4617
I think you can use:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Just give him the correct range.
Upvotes: 0