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.
Asked
Active
Viewed 3,750 times
1
-
NSNumberFormatter - https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html – some_id Jul 15 '12 at 20:40
-
Try the code i posted in the following answer - I updated it today because of a question by Umka: http://stackoverflow.com/a/2919532/250164 I realize it's not exactly the code you want, but I think it's a good example on how to apply a currency formatter mask to a text field. You can change the code for your purposes. – Wolfgang Schreurs Jul 15 '12 at 20:51
1 Answers
2
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.

David H
- 40,852
- 12
- 92
- 138