如何在编辑UITextField文本的同时对其进行实时格式化?

时间:2013-09-18 03:34:21

标签: ios objective-c

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

换句话说:

当我输入1时,它应显示1

当我键入10时,它应显示10

当我输入100时,它应显示100 但

当我输入1000时,它应显示1,000

你能说出任何想法吗?

5 个答案:

答案 0 :(得分:17)

在文本字段控件中添加“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;            
}

答案 1 :(得分:3)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] ;
    if([string length]==0)
    {
        [formatter setGroupingSeparator:@","];
        [formatter setGroupingSize:4];
        [formatter setUsesGroupingSeparator:YES];
        [formatter setSecondaryGroupingSize:3];
        NSString *num = textField.text ;
        num = [num stringByReplacingOccurrencesOfString:@"," withString:@""];
        NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
        textField.text = str;
        return YES;
    }
    else {
        [formatter setGroupingSeparator:@","];
        [formatter setGroupingSize:2];
        [formatter setUsesGroupingSeparator:YES];
        [formatter setSecondaryGroupingSize:3];
        NSString *num = textField.text ;
        if(![num isEqualToString:@""])
        {
            num = [num stringByReplacingOccurrencesOfString:@"," withString:@""];
            NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
            textField.text = str;
            NSLog(@"add:%@",str);
        }
        return YES;
    }
}

P.S。此代码位于ARC,负责非弧环境下的内存管理。

答案 2 :(得分:1)

添加操作:

self.amountTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

实际情况:

@objc func textFieldDidChange(textField: UITextField) {
    if textField == amountTextField {
        
        var textFormatted = textField.text?.replacingOccurrences(of: ".", with: "")
        textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")

        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        if let text = textFormatted, let intVal = Int(text) {
            textField.text = formatter.string(from: NSNumber(value: intVal))
        }
    }
}

答案 3 :(得分:0)

<强>夫特:

务必选择文本字段输入数字键盘,然后:

在viewDidLoad中:

yourTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)

然后,在同一个视图控制器中:

func textFieldDidChange(textField: UITextField) {
        if textField == yourTextField {

            // Some locales use different punctuations.
            var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
            textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")

            let numberFormatter = NumberFormatter()
            numberFormatter.numberStyle = .decimal
            if let text = textFormatted, let textAsInt = Int(text) {
                textField.text = numberFormatter.string(from: NSNumber(value: textAsInt))
            }
        }
    }

答案 4 :(得分:-2)

试试这个..您可以在ViewDidLoad方法中编写以下行并将代理人提供给文本字段。

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

并编写以下方法。

-(void)textFieldDidChange:(UITextField*)textfield{
  NSLog(@"here %@",textfield.text);
  if ([textfield.text isEqualToString:@"1000"]) {
    textfield.text=@"1,000";
  }
}