在UITextField中用逗号分隔的数字不起作用 [英] Numbers separated by commas in UITextField not working

查看:138
本文介绍了在UITextField中用逗号分隔的数字不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每3个字符后添加一个小数。 (倒数计数如下:1,325,541而不是1325451。)

I'm trying to add a decimal after every 3 characters. (Counting backwards like this: 1,325,541 instead of 1325451.)

这是我尝试过的:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSNumberFormatter *numberFormat = [[NSNumberFormatter alloc] init];
    [numberFormat setGroupingSeparator:@","];
    [numberFormat setGroupingSize:3];
    [numberFormat setNumberStyle:NSNumberFormatterDecimalStyle];

    NSNumber *amount = [numberFormat numberFromString:textField.text];
    textField.text = [numberFormat stringFromNumber:amount];
    return YES;
}

每3个字符后不插入逗号。我该怎么办才能修复它?

It doesn't insert a comma after every 3 characters. What can I do to fix it?

推荐答案

嗯,这最终会比你想象的更微妙,但是这个是我提出的:

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

在@ bgfriend0的评论中提到, textField:shouldChangeCharactersInRange:replacementString 在申请之前被调用对文本字段的编辑(由于其他原因,这很有用)。这意味着,如果您当前的字符串是 123 并且用户输入 4 ,则该方法将被传递给以下作为参数:

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: textField,但请注意这一点 textField.text 123

  • 范围: NSRange {2,0}

  • 字符串: 4

  • 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).

实例化textField后,添加目标以收听编辑事件:

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

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

只要在 UITextField 。我们将执行的 SEL 将如下所示:

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.

至于 textField:shouldChangeCharactersInRange:replacementString ,这是进行字符验证的更好的地方。因此,检查字母的基本验证可能如下所示:

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

因此,使用这两位代码,您将能够更新textfield字符串包含逗号每隔3个字符,并且用户将无法输入任何字母表中的任何字母(但他们仍然可以输入其他'非法'字符,因此请根据需要扩展该验证。)

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).

这篇关于在UITextField中用逗号分隔的数字不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆