Re-Apply currency formatting to a UITextField on a change event

I've been working on this issue and I think I figured out a nice, clean solution. I'll show you how to appropriately update the textfield on user input, but you'll have to figure out the localization yourself, that part should be easy enough anyway.

- (void)viewDidLoad
{
    [super viewDidLoad];


    // setup text field ...

#define PADDING 10.0f

    const CGRect bounds = self.view.bounds;
    CGFloat width       = bounds.size.width - (PADDING * 2);
    CGFloat height      = 30.0f;
    CGRect frame        = CGRectMake(PADDING, PADDING, width, height);

    self.textField                      = [[UITextField alloc] initWithFrame:frame];
    _textField.backgroundColor          = [UIColor whiteColor];
    _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _textField.autocapitalizationType   = UITextAutocapitalizationTypeNone;
    _textField.autocorrectionType       = UITextAutocorrectionTypeNo;
    _textField.text                     = @"0";
    _textField.delegate                 = self;
    [self.view addSubview:_textField];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    // force update for text field, so the initial '0' will be formatted as currency ...

    [self textField:_textField shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@"0"];
}

- (void)viewDidUnload
{
    self.textField = nil;

    [super viewDidUnload];
}

This is the code in the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacementString:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *text             = _textField.text;
    NSString *decimalSeperator = @".";
    NSCharacterSet *charSet    = nil;
    NSString *numberChars      = @"0123456789";


    // the number formatter will only be instantiated once ...

    static NSNumberFormatter *numberFormatter;
    if (!numberFormatter)
    {
        numberFormatter = [[NSNumberFormatter alloc] init];
        numberFormatter.numberStyle           = NSNumberFormatterCurrencyStyle;
        numberFormatter.maximumFractionDigits = 10;
        numberFormatter.minimumFractionDigits = 0;
        numberFormatter.decimalSeparator      = decimalSeperator;
        numberFormatter.usesGroupingSeparator = NO;
    }


    // create a character set of valid chars (numbers and optionally a decimal sign) ...

    NSRange decimalRange = [text rangeOfString:decimalSeperator];
    BOOL isDecimalNumber = (decimalRange.location != NSNotFound);
    if (isDecimalNumber)
    {
        charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];        
    }
    else
    {
        numberChars = [numberChars stringByAppendingString:decimalSeperator];
        charSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
    }


    // remove amy characters from the string that are not a number or decimal sign ...

    NSCharacterSet *invertedCharSet = [charSet invertedSet];
    NSString *trimmedString = [string stringByTrimmingCharactersInSet:invertedCharSet];
    text = [text stringByReplacingCharactersInRange:range withString:trimmedString];


    // whenever a decimalSeperator is entered, we'll just update the textField.
    // whenever other chars are entered, we'll calculate the new number and update the textField accordingly.

    if ([string isEqualToString:decimalSeperator] == YES) 
    {
        textField.text = text;
    }
    else 
    {
        NSNumber *number = [numberFormatter numberFromString:text];
        if (number == nil) 
        {
            number = [NSNumber numberWithInt:0];   
        }
        textField.text = isDecimalNumber ? text : [numberFormatter stringFromNumber:number];
    }

    return NO; // we return NO because we have manually edited the textField contents.
}

Edit 1: fixed memory leak.

Edit 2: updated for Umka - handling of 0 after decimal separator. Code is updated for ARC as well.


This was good basis but still did not satisfy my app needs. Here is what I cooked to help it. I understand that code is actually cumbersome and it is here rather to give a clue and may be to get more elegant solution.

- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{   
        if (aTextField == myAmountTextField) {
                NSString *text = aTextField.text;
                NSString *decimalSeperator = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
                NSString *groupSeperator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

                NSCharacterSet *characterSet = nil;
                NSString *numberChars = @"0123456789";
                if ([text rangeOfString:decimalSeperator].location != NSNotFound)
                        characterSet = [NSCharacterSet characterSetWithCharactersInString:numberChars];
                else
                        characterSet = [NSCharacterSet characterSetWithCharactersInString:[numberChars stringByAppendingString:decimalSeperator]];

                NSCharacterSet *invertedCharSet = [characterSet invertedSet];   
                NSString *trimmedString = [string stringByTrimmingCharactersInSet:invertedCharSet];
                text = [text stringByReplacingCharactersInRange:range withString:trimmedString];

                if ([string isEqualToString:decimalSeperator] == YES ||
                    [text rangeOfString:decimalSeperator].location == text.length - 1) {
                        [aTextField setText:text];
                } else {
                        /* Remove group separator taken from locale */
                        text = [text stringByReplacingOccurrencesOfString:groupSeperator withString:@""];


                        /* Due to some reason, even if group separator is ".", number
                        formatter puts spaces instead. Lets handle this. This all should
                        be done before converting to NSNUmber as otherwise we will have
                        nil. */
                        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
                        NSNumber *number = [numberFormatter numberFromString:text];
                        if (number == nil) {
                                [textField setText:@""];
                        } else {
                                /* Here is what I call "evil miracles" is going on :)
                                Totally not elegant but I did not find another way. This
                                is all needed to support inputs like "0.01" and "1.00" */
                                NSString *tail = [NSString stringWithFormat:@"%@00", decimalSeperator];
                                if ([text rangeOfString:tail].location != NSNotFound) {
                                        [numberFormatter setPositiveFormat:@"#,###,##0.00"];
                                } else {
                                        tail = [NSString stringWithFormat:@"%@0", decimalSeperator];
                                        if ([text rangeOfString:tail].location != NSNotFound) {
                                                [numberFormatter setPositiveFormat:@"#,###,##0.0#"];
                                        } else {
                                                [numberFormatter setPositiveFormat:@"#,###,##0.##"];
                                        }
                                }
                                text = [numberFormatter stringFromNumber:number];
                                [textField setText:text];
                        }
                }

                return NO;
        }
        return YES;
}

Number formatter is initialized in a way like this:

numberFormatter = [[NSNumberFormatter alloc] init]; 
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.roundingMode = kCFNumberFormatterRoundFloor;

It is locale aware, so should work fine in different locale setups. It also supports the following inputs (examples):

0.00 0.01

1,333,333.03

Please somebody improve this. Topic is kind of interesting, no elegant solution exists so far (iOS has not setFormat() thing).


Here's my version of this.

Setup a formatter some place:

    // Custom initialization
    formatter = [NSNumberFormatter new];
    [formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    [formatter setLenient:YES];
    [formatter setGeneratesDecimalNumbers:YES];

Then use it to parse and format the UITextField:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *replaced = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSDecimalNumber *amount = (NSDecimalNumber*) [formatter numberFromString:replaced];
    if (amount == nil) {
        // Something screwed up the parsing. Probably an alpha character.
        return NO;
    }
    // If the field is empty (the inital case) the number should be shifted to
    // start in the right most decimal place.
    short powerOf10 = 0;
    if ([textField.text isEqualToString:@""]) {
        powerOf10 = -formatter.maximumFractionDigits;
    }
    // If the edit point is to the right of the decimal point we need to do
    // some shifting.
    else if (range.location + formatter.maximumFractionDigits >= textField.text.length) {
        // If there's a range of text selected, it'll delete part of the number
        // so shift it back to the right.
        if (range.length) {
            powerOf10 = -range.length;
        }
        // Otherwise they're adding this many characters so shift left.
        else {
            powerOf10 = [string length];
        }
    }
    amount = [amount decimalNumberByMultiplyingByPowerOf10:powerOf10];

    // Replace the value and then cancel this change.
    textField.text = [formatter stringFromNumber:amount];
    return NO;
}