如何在键入时更改UITextField文本的值?换句话说:当我输入1时,它应该显示1当我输入10时,它应该显示10当我输入100时,它应该显示100,但当我输入1000时,它应该显示1,000你能给给予点建议吗?
UITextField
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; }
字符串
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环境下的内存管理。
ARC
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)) } } }
型
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)) } } }
ebdffaop5#
1.确保textField.keyboardType = .numberPad1.实现ExtFieldDelegate方法:
textField.keyboardType = .numberPad
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 }
ha5z0ras6#
试试这个。你可以在ViewDidLoad方法中写下面一行,然后给予delegate给文本字段。
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"; } }
6条答案
按热度按时间kuarbcqp1#
向文本字段控件添加“textFieldDidChange”通知方法。
字符串
ecr0jaav2#
字符串
**P.S.**此代码在
ARC
中,注意非arc环境下的内存管理。rqdpfwrv3#
Swift:
确保选择文本字段输入作为数字键盘,然后:
在您的viewDidLoad中:
字符串
然后,在同一个视图控制器中:
型
7kqas0il4#
添加操作:
字符串
在行动中:
型
ebdffaop5#
1.确保
textField.keyboardType = .numberPad
1.实现ExtFieldDelegate方法:
字符串
ha5z0ras6#
试试这个。你可以在
ViewDidLoad
方法中写下面一行,然后给予delegate给文本字段。字符串
并编写以下方法。
型