Numbers separated by commas in UITextField not working

 ̄綄美尐妖づ 提交于 2019-12-02 10:05:28

Well, this ends up being a little more nuanced than you might expect, but this is what I came up with:

As @bgfriend0's comment mentions, textField:shouldChangeCharactersInRange:replacementString gets called before applying an edit to a text field (this is useful for other reasons). Meaning, that if your current string was 123 and a user goes to type in 4, the method would be passed the following as parameters:

  • textField: the textField, but note at this point textField.text is 123
  • range: NSRange{2, 0}
  • string: 4

And there is a way to make this function work from within that method, but there's a better way (I think).

After you instantiate your textField, add a target to listen for editing events:

[textField addTarget:self 
              action:@selector(formatNumberIfNeeded:)    
    forControlEvents:UIControlEventEditingChanged];

This will get called anytime an editing change happens in a UITextField. The SEL that we'll be performing will look like this:

- (void)formatNumberIfNeeded:(UITextField *)textField{
    // you'll need to strip the commas for the formatter to work properly
    NSString * currentTextWithoutCommas = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];

    NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;

    NSNumber * numberFromString = [numberFormatter numberFromString:currentTextWithoutCommas];
    NSString * formattedNumberString = [numberFormatter stringFromNumber:numberFromString];

    textField.text = formattedNumberString;
}

Now, things get slightly more tricky if you need to localize, but cross that bridge if needed.

As for textField:shouldChangeCharactersInRange:replacementString, that's a much better place to do character validation. So, a basic validation to check for letters could look like:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSRange illegalCharacterEntered = [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
    if ( illegalCharacterEntered.location != NSNotFound ) {
        return NO;
    }

    return YES;
}

So with those two bits of code, you'll be able to update the textfield string to include a comma every 3rd character, and users wont be able to enter any letters of the alphabet (but they can still input other 'illegal' characters, so extend that validation as needed).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!