0

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.

Eric
  • 3,811
  • 5
  • 15
  • 18
  • what about adding an `-(IBAction)` and hook that up with "Value Changed" event of the textfield. – Ayan Sengupta Jun 09 '15 at 21:17
  • Thanks for the reply. I tried that, and I didn't get any results from the method. I tried an NSLog in that method, and nothing came up – Eric Jun 09 '15 at 21:52
  • My bad! For a `UITextField`, it should have been "Editing changed" event rather than the "Value Changed" – Ayan Sengupta Jun 09 '15 at 21:58
  • No problem! I tried that, and it gives me the same results as `shouldChangeTextInRange` – Eric Jun 09 '15 at 22:06
  • Try using the notification for key pressed event of keyboard. `//set notification for when a key is pressed. [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil]; }` – MPG Jun 10 '15 at 06:24
  • Thanks for responding. Here is a sample project that might help understand what the problem is: https://jumpshare.com/b/PSW9i0N012RSgzFoW6Eb – Eric Jun 10 '15 at 06:25
  • Refer this http://stackoverflow.com/questions/5036971/find-locale-currency-for-iphone-programmatically – MPG Jun 10 '15 at 06:53
  • Thanks for the link!! It says how to set the currency to the current one. That's only half of my question. How can I apply that to a UITextField that is editiable? – Eric Jun 10 '15 at 07:14

1 Answers1

0

If you are using ReactiveCocoa you can try doing this.

[textField.rac_textSignal subscribeNext:^(NSString *text) {
    if (text.length < 4) text = @"0.00";

    //set currency style
    NSNumberFormatter *currencyFormatter = [NSNumberFormatter new];
    currencyFormatter.numberStyle = NSNumberFormatterCurrencyStyle;

    //leave only decimals (we want to get rid of any unwanted characters)

    NSString *decimals = [[text componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

    //insert decimal separator
    NSMutableString *mutableString = [NSMutableString stringWithString:decimals];
    [mutableString insertString:currencyFormatter.decimalSeparator atIndex:mutableString.length - currencyFormatter.minimumFractionDigits];

    //I add currency symbol so that formatter recognizes decimal separator while formatting to NSNumber
    NSString *result = [currencyFormatter.currencySymbol stringByAppendingString:mutableString];

    NSNumber *formattedNumber = [currencyFormatter numberFromString:result];
    NSString *formattedText = [currencyFormatter stringFromNumber:formattedNumber];

    //saving cursors position 
    UITextRange *position = textField.selectedTextRange;

    textField.text = formattedText;

    //reassigning cursor position (Its not working properly due to commas etc.)
    textField.selectedTextRange = position;
}];

It's not perfect, but maybe this can help you a bit in finding correct solution.

Eluss
  • 512
  • 3
  • 5