关于ios:如何限制UITextField中的字符

How to limit characters in UITextField

本问题已经有最佳答案,请猛点这里访问。

我的UITextField具有以下限制-字符只能是数字,如下面的方法所示。 我还希望将最大字符数限制为3位数字。 如何修改我的方法来做到这一点?

1
2
3
4
5
6
7
8
9
10
11
12
- (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;
}


o限制文本字段在以下代码中使用。

1
2
3
4
5
6
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
     NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];    
        return !([newString length] > 3);

}


您的代码已关闭。 尝试:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (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;
}


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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中的数字验证

1
2
3
4
5
6
7
8
9
10
11
12
13
#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;
}

您可以将观察者添加到UITextFieldTextDidChangeNotification

//在viewDidLoad中添加此行

1
2
3
4
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(limitTextField) name:UITextFieldTextDidChangeNotification object:nil];

//dealloc add this
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

//限制文本字段字符的方法

1
2
3
4
-(void)limitTextField
{
   textField.text = [textField.text substringToIndex:3];
}

//或设置文本字段的委托并使用它

1
2
3
4
5
6
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]<=3)
   return YES;
else
   return NO;
}

并将文本字段的keyBoardType设置为UIKeyboardTypeNumberPad以仅获取数字。 我没有测试或编译代码,只是编写了逻辑。 希望能帮助到你 :)


用这种方式声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #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:@"
"
]) {
        [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;
}

只允许数字,你也可以这样定义

1
[textField setKeyboardType:UIKeyboardTypeNumberPad];

尝试这个:

1
2
3
4
5
6
7
8
- (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];
}