Re-Apply currency formatting to a UITextField on a change event

Posted by Jim Aldes on Stack Overflow See other posts from Stack Overflow or by Jim Aldes
Published on 2010-03-05T16:56:39Z Indexed on 2010/05/27 8:31 UTC
Read the original article Hit count: 466

Howdy all,

I'm working with a UITextField that holds a localized currency value. I've seen lots of posts on how to work with this, but my question is: how do I re-apply currency formatting to the UITextField after every key press?

I know that I can set up and use a currency formatter with:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
...
[currencyFormatter stringFromNumber:...];

but I don't know how to hook it up.

For instance, if the value in the field reads "$12,345" and the user taps the "6" key, then the value should change to "$123,456".

Which callback is the "correct" one to do this in (should I use textField:shouldChangeCharactersInRange:replacementString: or a custom target-action) and how do I use the NSNumberFormatter to parse and re-apply formatting to the UITextField's text property?

Any help would be much appreciated! Thanks!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uitextfield