1

I have a UITextField in which the user enters an amount of money. I am trying to make the textField show the current money symbol, as the user types in an amount.

So far I was able to do that in textFieldDIdEndEditing:, how can I do the following code in a method that lets you change as the user types. (For example shouldChangeTextInRange:.) This way the currency will always show, not only when the user is finished entering the amount.

Here is what I have so far:

- (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;
}

The reason why I don't just insert it in a UILabel is because some places have their currency after the amount. (Ex. France)

Eric
  • 3,811
  • 5
  • 15
  • 18

2 Answers2

0

Create a UIImageView with currency image.

And then set leftView either RightView of UITextField as per your requirement.

UIImage *cImage = [UIImage imageNamed:@"currency.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cImage];
[imageView setFrame:CGRectMake(0, 0, 40, 40)];

// For Left
[textfield setLeftView:imageView];

// or

// For Right

[textfield setRightView:imageView];
Vikas Pandey
  • 550
  • 4
  • 13
  • Thanks for the answer! Btw, you can do it with a `UILabel`, just saying. Also, how can I detect if the symbol should be on the left of right side? – Eric Jun 09 '15 at 09:03
0

Try this.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == txtAmount) {
        if ([txtAmount.text isEqualToString:@""]) {
            txtAmount.text = @"$";
        }else{
            txtAmount.text = [NSString stringWithFormat:@"%@",txtAmount.text];
        }
    }

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField == txtAmount) {
        if ([txtAmount.text isEqualToString:@"$"]) {
            txtAmount.placeholder = @"Amount";
        }
    }
    return YES;
}

and don't forget to give delegate to the UITextField.

Keyur Akbari
  • 182
  • 1
  • 13
  • Thanks for the answer, but my question was how do I make it the current currency. For example, euros or dollars etc – Eric Jun 09 '15 at 16:53
  • Please see this http://stackoverflow.com/questions/5036971/find-locale-currency-for-iphone-programmatically – Keyur Akbari Jun 10 '15 at 05:54
  • I checked it up. It's very useful!! That solves half the problem. The other half would be to implement that in a textField. Here is a project, that may explain better what I mean: https://jumpshare.com/b/PSW9i0N012RSgzFoW6Eb – Eric Jun 10 '15 at 06:04