如何限制UITextField中的字符

时间:2013-12-08 02:36:47

标签: ios iphone objective-c cocoa-touch uitextfield

我有一个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;
}

7 个答案:

答案 0 :(得分:13)

o限制代码下方的文本字段使用。

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

}

答案 1 :(得分:4)

你的代码很接近。尝试:

- (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;
}

答案 2 :(得分:2)

您可以将观察者添加到UITextFieldTextDidChangeNotification

//在viewDidLoad

中添加此行
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(limitTextField) name:UITextFieldTextDidChangeNotification object:nil];

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

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

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

//或者设置textfield的委托并使用此

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

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

答案 3 :(得分:2)

  

设置最大字符限制&amp; UITextField中的数字验证

     

在UITextField中设置最大字符数限制: -   我们不能直接将最大长度设置为UITextField,因为它的类没有max length属性,但可以使用以下文本字段委托方法来实现。

#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;
   }
}
  

在文本字段更改之前,UITextField会询问委托是否应更改指定的文本。此时文本字段没有改变,所以我们抓住它的当前长度和我们插入的字符串长度减去范围长度。如果此值太长(在此示例中超过20个字符),则返回NO以禁止更改。

<强>&GT; 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;
}

答案 4 :(得分:0)

试试这个:

- (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];
}

答案 5 :(得分:0)

以这种方式声明

 #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;
}

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

[textField setKeyboardType:UIKeyboardTypeNumberPad];

答案 6 :(得分:0)

这肯定有帮助。

这是重复的回答:  去检查一下 回答 Set the maximum character length of a UITextField