Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put currency symbol while and after typing in UITextfield?

I have this stock market calculator that I am working on and I searched Apple documentation, the internet, here at StackOverFlow, but wasn't successful in finding the answer..

I have a UITextfield in which the user will input a currency value. What I want to implement is when the user is typing or at least after he finishes type the value the textfield would also display the currency symbol corresponding to the locale he is.

It's like a placeholder, but not the one we have in xcode, cause xcode's is there before we type and the one I want should be there while typing and after it. I could use a background image with the currency in it, but then I wouldn't be able to localize the app.

So if any one could help, I would appreciate.

Thanks in advance.

like image 344
Marco Almeida Avatar asked Nov 22 '12 21:11

Marco Almeida


2 Answers

You have to use NSNumberFormatter to achieve this.

Try the following code, and by this, once you entered the values and when you end editing, the values will be formatted with 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;
}
like image 93
Ilanchezhian Avatar answered Oct 27 '22 07:10

Ilanchezhian


The easiest way would be to put a label with right aligned text up against your text field, which would have left aligned text.

When the user starts to edit the textfield, set the currency symbol:

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

If you want to keep it as part of the text in the textField, it becomes a little more complicated as you need to keep them from deleting the symbol once you put it there:

// 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 be formatting the currency by using a number formatter since you are displaying it to the user.

like image 22
lnafziger Avatar answered Oct 27 '22 09:10

lnafziger