ios – 在编辑UITextField文本时,如何实时格式化?

输入时如何更改UITextField文本的值?

换句话说:

当我输入1时应显示1

当我键入10它应该显示10

当我输入100它应该显示100

当我键入1000,它应该显示1000

你能给任何想法吗?

解决方法

向文本字段控件添加“textFieldDidChange”通知方法.
[textField addTarget:self
              action:@selector(textFieldDidChange:)
    forControlEvents:UIControlEventEditingChanged];


-(void)textFieldDidChange:(UITextField *)theTextField
{
    NSLog(@"text changed: %@",theTextField.text);

    NSString *textFieldText = [theTextField.text stringByReplacingOccurrencesOfString:@"," withString:@""];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *formattedOutput = [formatter stringFromNumber:[NSNumber numberWithInt:[textFieldText integerValue]]];
    textField.text=formattedOutput;            
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...