ios 如何限制 UITextField 中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20449210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to limit characters in UITextField
提问by venki
I have a UITextField
with the following restrictions - characters can only be numbers, as in the method below. I want to also limit the number of max characters to 3 digits. How can I modify my method to do that?
我有UITextField
以下限制 - 字符只能是数字,如下面的方法。我还想将最大字符数限制为 3 位数。我该如何修改我的方法来做到这一点?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [string length]; i++) {
unichar aCharacter = [string characterAtIndex:i];
if ([aCharacterSet characterIsMember:aCharacter]) {
return YES;
}
}
NSUInteger newLength = self.textField.text.length + string.length - range.length;
return (newLength > 3) ? NO : YES;
}
回答by venki
o limit the text field use below code.
o 限制使用下面的代码的文本字段。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] > 3);
}
回答by rmaddy
Your code is close. Try:
你的代码很接近。尝试:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
for (int i = 0; i < [string length]; i++) {
unichar aCharacter = [string characterAtIndex:i];
if (![aCharacterSet characterIsMember:aCharacter]) {
return NO;
}
}
NSUInteger newLength = self.textField.text.length + string.length - range.length;
return (newLength > 3) ? NO : YES;
}
回答by sanjaymathad
You can add an observer to UITextFieldTextDidChangeNotification
您可以将观察者添加到 UITextFieldTextDidChangeNotification
// add this line in viewDidLoad
// 在 viewDidLoad 中添加这一行
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(limitTextField) name:UITextFieldTextDidChangeNotification object:nil];
//dealloc add this
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
//method to limit textfield's characters
//限制文本框字符的方法
-(void)limitTextField
{
textField.text = [textField.text substringToIndex:3];
}
//or else set delegate of textfield and use this
//或者设置文本字段的委托并使用它
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]<=3)
return YES;
else
return NO;
}
and set the keyBoardType
of textfield as UIKeyboardTypeNumberPad
to get only numbers. I have not tested or compiled the code, just wrote the logic. Hope it helps :)
并将keyBoardType
文本字段设置为 UIKeyboardTypeNumberPad
仅获取数字。我没有测试或编译代码,只是写了逻辑。希望能帮助到你 :)
回答by Abdul Karim
Setting Maximum Character Limit & Numeric Validation in UITextField
Setting Maximum Character Limit in UITextField:- We can't directly set the maximum length to the UITextField because it's class has no max length property, But it can be achieved by using the following text field delegate method.
在 UITextField 中设置最大字符限制和数字验证
在 UITextField 中设置最大字符限制:- 我们不能直接为 UITextField 设置最大长度,因为它的类没有最大长度属性,但可以通过使用以下文本字段委托方法来实现。
#define MAX_LENGTH 20
– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] – range.length;
if(newLength > MAX_LENGTH){
return NO;
}
else{
return YES;
}
}
Before the text field changes, the UITextField asks the delegate if the specified text should be changed. The text field has not changed at this point, so we grab it's current length and the string length we're inserting, minus the range length. If this value is too long (more than 20 characters in this example), return NO to prohibit the change.
在文本字段更改之前,UITextField 会询问委托是否应更改指定的文本。此时文本字段没有改变,所以我们获取它的当前长度和我们插入的字符串长度,减去范围长度。如果这个值太长(在这个例子中超过 20 个字符),返回 NO 以禁止更改。
> Numeric Validation in UITextField
> UITextField 中的数字验证
#define NUMBERS_ONLY 0123456789
– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
if([textField isEqual:txtPhone])
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
NSString *filteredstring = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@””];
return ([string isEqualToString:filteredstring]);
}
return TRUE;
}
回答by Banker Mittal
declare this way
这样声明
#define MAX_LENGTH_FULLNAME 3
#pragma mark - TextField Delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if([string length]==0){
return YES;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if ([string isEqualToString:@"\n"]) {
[self.txtAnswer resignFirstResponder];
return NO;
}
if (textField == self.txtFName){
/* for backspace */
if([string length]==0){
return YES;
}
if (newLength > MAX_LENGTH_FULLNAME) {
if([string length] > MAX_LENGTH_FULLNAME){
textField.text = [string substringToIndex:MAX_LENGTH_FULLNAME];
}
return NO;
}
NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if ([myCharSet characterIsMember:c]) {
return YES;
}
}
return NO;
}
return YES;
}
to allow only numbers you can also just define this way
只允许数字你也可以这样定义
[textField setKeyboardType:UIKeyboardTypeNumberPad];
回答by user523234
Try this:
尝试这个:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *aCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:aCharacterSet] componentsJoinedByString:@""];
if ((textField.text.length >2) && (!(string.length == 0)))
return NO;
else
return [string isEqualToString:filtered];
}