[英]How do I format UITextField text in real time, while it's being edited?
How can I change value of UITextField
text while typing?如何在键入时更改
UITextField
文本的值?
In other word:换句话说:
when I type 1 it should show 1当我输入 1 时它应该显示 1
when I type 10 it should show 10当我输入 10 时,它应该显示 10
when I type 100 it should show 100 but当我输入 100 时,它应该显示 100 但
when I type 1000 it should show 1,000当我输入 1000 时,它应该显示 1,000
Can you give any idea?你能给出任何想法吗?
Add a "textFieldDidChange" notification method to the text field control.向文本字段控件添加“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;
}
- (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 This code is in ARC
, take care of memory management in non arc environment. PS此代码在
ARC
,负责非 arc 环境中的内存管理。
Swift:迅速:
Be sure to select the text field input as number pad , then:请务必选择文本字段输入作为数字键盘,然后:
In your viewDidLoad:在您的 viewDidLoad 中:
yourTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
Then, in that same view controller:然后,在同一个视图控制器中:
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))
}
}
}
add action:添加动作:
self.amountTextField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
in action:在行动:
@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))
}
}
}
Try this one.. You can write following line in ViewDidLoad
method and give delegate to the textfield.试试这个..您可以在
ViewDidLoad
方法中编写以下行并将委托委托给文本字段。
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
and write the following method.并写出以下方法。
-(void)textFieldDidChange:(UITextField*)textfield{
NSLog(@"here %@",textfield.text);
if ([textfield.text isEqualToString:@"1000"]) {
textfield.text=@"1,000";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.