ross_t
ross_t

Reputation: 511

How do I apply a numeric input mask to a UITextField in Objective-c

I have a TextField which I would only like the end user to enter monetary values into, e.g. 20000. I would like the field to automatically display the entered numbers with a display mask, so that when the user enters the keystrokes it changes the field contents dynamically. E.g. 2000050 entered would display as 20,000.50.

Upvotes: 1

Views: 3780

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

You use the

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

delegate method. In the method look at what the string currently is (textField.text), what it will be after applying the replacement string, then create your own formatted string and set it using textField.text = "20,000.50" etc. Return NO since you handled the text.

Upvotes: 2

Related Questions