How to put a currency symbol during and after input in a UITextfield?

I have a stock market calculator that I'm working on, and I searched the Apple documentation online, here in StackOverFlow, but couldn't find the answer.

I have a UITextfield in which the user enters a currency value. What I want to implement is when the user types, or at least after he finishes, enter a value that the text box will also display a currency symbol corresponding to his language.

It's like a placeholder, but not the one we have in xcode, because xcode is there before we print, and the one I want should be there when we print and after it. I could use a background image with currency in it, but then I could not localize the application.

Therefore, if anyone can help, I would appreciate it.

Thanks in advance.

+8
ios objective-c uitextfield currency ios5
source share
2 answers

The easiest way is to place a label with the correct aligned text against your text box, which would leave the aligned text.

When the user starts editing the text field, set the currency symbol:

  - (void)textFieldDidBeginEditing:(UITextField *)textField { self.currencyLabel.text = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]; } 

If you want to save it as part of the text in a text field, it will become a little more complicated, since you need them to not delete the character after placing it:

 // Set the currency symbol if the text field is blank when we start to edit. - (void)textFieldDidBeginEditing:(UITextField *)textField { if (textField.text.length == 0) { textField.text = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]; } } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string]; // Make sure that the currency symbol is always at the beginning of the string: if (![newText hasPrefix:[[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol]]) { return NO; } // Default: return YES; } 

As @Aadhira points out, you should also format the currency using the number format, since you are showing it to the user.

+5
source share

To achieve this, you must use NSNumberFormatter .

Try using the following code, and after that, as soon as you enter the values, and when you finish editing, the values ​​will be formatted with the current currency.

 -(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; } 
+3
source share

All Articles