I have a UITextField
that the user will enter an amount of money. I want to set it so it will show the users current currency. I could do the following:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setLocale:[NSLocale currentLocale]];
[currencyFormatter setMaximumFractionDigits:2];
[currencyFormatter setMinimumFractionDigits:2];
[currencyFormatter setAlwaysShowsDecimalSeparator:YES];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *someAmount = [NSNumber numberWithDouble:[textField.text doubleValue]];
NSString *string = [currencyFormatter stringFromNumber:someAmount];
textField.text = string;
}
That works. But I want it to show on startup, and while the user is typing the amount. The above code only works when the user is finished with that textField. How can I make the code in that method to show on startup and while the user is entering numbers.
I tried to change the method to shouldChangeTextInRange
, but it gives a weird effect.
If you are using ReactiveCocoa you can try doing this.
It's not perfect, but maybe this can help you a bit in finding correct solution.