开发者

UITextField > force cursor to stay at the end of the field

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开发者_StackOverflow 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?


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.


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.


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.


I think you can use:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Just give him the correct range.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜