ios 如何在编辑文本的同时,真实的格式化extField文本?

oprakyz7  于 2023-11-20  发布在  iOS
关注(0)|答案(6)|浏览(124)

如何在键入时更改UITextField文本的值?
换句话说:
当我输入1时,它应该显示1
当我输入10时,它应该显示10
当我输入100时,它应该显示100,但
当我输入1000时,它应该显示1,000
你能给给予点建议吗?

kuarbcqp

kuarbcqp1#

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

字符串

ecr0jaav

ecr0jaav2#

- (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中,注意非arc环境下的内存管理。

rqdpfwrv

rqdpfwrv3#

Swift:

确保选择文本字段输入作为数字键盘,然后:
在您的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))
            }
        }
    }

7kqas0il

7kqas0il4#

添加操作:

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

ebdffaop

ebdffaop5#

1.确保textField.keyboardType = .numberPad
1.实现ExtFieldDelegate方法:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

guard let text = textField.text else {
    return true
}

let modifiedText = (text as NSString).replacingCharacters(in: range, with: string)

let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .decimal
formatter.groupingSeparator = ","

let modifiedTextWithoutGroupingSeparator = modifiedText.replacingOccurrences(of: formatter.groupingSeparator, with: "")

if let formattedNumber = formatter.number(from: modifiedTextWithoutGroupingSeparator) {
    textField.text = formatter.string(from: formattedNumber)
}

return false
}

字符串

ha5z0ras

ha5z0ras6#

试试这个。你可以在ViewDidLoad方法中写下面一行,然后给予delegate给文本字段。

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

相关问题