13

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

换句话说:

当我输入 1 它应该显示 1

当我输入 10 它应该显示 10

当我输入 100 它应该显示 100 但是

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

你能给个主意吗?

4

5 回答 5

18

向文本字段控件添加“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;            
}
于 2013-09-18T05:21:27.707 回答
4
- (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;
    }
}

PS这段代码在ARC,注意非 arc 环境中的内存管理。

于 2013-09-18T05:37:07.963 回答
1

迅速:

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

在您看来DidLoad:

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))
            }
        }
    }
于 2017-03-30T01:12:05.183 回答
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))
        }
    }
}
于 2020-09-28T04:09:20.167 回答
-3

试试这个..您可以在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";
  }
}
于 2013-09-18T05:22:50.353 回答