Using a NSNumberFormatter with a UITextField
Using a NSNumberFormatter with a UITextField
- Subject: Using a NSNumberFormatter with a UITextField
- From: Ryan Stephens <email@hidden>
- Date: Tue, 2 Mar 2010 21:53:20 -0800
Hello list,
I'm currently trying to use an NSNumberFormatter to format the text of
a UITextField, so that as a user enters numbers into a text field, I
can apply currency-style formatting to the text (ex. add a properly
localized currency symbol/number separators). Currently I'm
implementing -textField:shouldChangeCharactersInRange:replacementString:
in the UITextField Delegate to intercept the input as the user enters
it and pass the textField text to a number formatter, then
(eventually) setting the textField text to the return value from the
formatter.
The issue I'm running in to is that the NSNumberFormatter seems to
only return empty values when I call numberFromString: or
stringFromNumber:, so I can't seem to get the expected text to the
textField.
I've done a bit of googling, searched through the archives and forums,
and can't seem to find many good examples of using an
NSNumberFormatter for currency and for using an NSFormatter with
UITextFields. I think the issue I may currently have is that my
formatter is improperly configured. I've looked over the Data
Formatting Programming Guide and the NSNumberFormatter Class Reference
docs, but they don't seem to go into much detail on how to properly
configure a NumberFormatter for use, or really cover the use cases I'm
trying to implement. Looking over the docs for NSFormatter and
NSDateFormatter doesn't seem to provide many hints either. Can anyone
point me to an example on working through these (or similar) problems?
In case anyone is curious, I've pasted my current implementation of
textField:ShouldChangeCharactersInRange:replacementString: below.
Thanks,
Ryan
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init]
autorelease];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setGeneratesDecimalNumbers:YES];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:2];
[formatter setAlwaysShowsDecimalSeparator:YES];
NSLog(@"text field content: %@", textField.text);
//The following line always seems to print (null)
NSLog(@"output of formatter: %@", [formatter
numberFromString:textField.text]);
//since [formatter numberFromString:textField.text] returns null,
the next line isn't working properly.
//textField.text = [[formatter numberFromString:textField.text]
stringValue];
return YES;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden