UITextField限制字符的数量和类型

时间:2012-09-04 14:27:38

标签: iphone objective-c ios

我有两个文本字段,我想限制字符的数量和类型。我已经使用以下代码来单独执行每个函数,但无法找到在同一函数中执行这两个函数的方法。

限制字符类型:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Only characters in the NSCharacterSet you choose will insertable.
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

并限制字符数:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 if (textField.text.length >= 10 && range.length == 0)
 return NO;
return YES;
}

3 个答案:

答案 0 :(得分:20)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= 10 && range.length == 0)
       return NO;
    // Only characters in the NSCharacterSet you choose will insertable.
    NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

<强>被修改

如果你想为第三个文字字段添加不同的条件,你可以这样做。
为第三个文本fild说thirdField

创建参考

然后使用此

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == thirdField) {
        //your contion e.g
        if (textField.text.length < 7) {
            return YES;
        } else {
            return NO;
        }        
    }
    else {
        if (textField.text.length >= 10 && range.length == 0)
            return NO;
        // Only characters in the NSCharacterSet you choose will insertable.
        NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
}

答案 1 :(得分:3)

这是限制在UITextField中输入的字符的最简洁方法之一。这种方法允许使用多个预定义的NSCharacterSets。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSMutableCharacterSet *allowedCharacters = [NSMutableCharacterSet alphanumericCharacterSet];
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
    [allowedCharacters addCharactersInString:@":./"];  //allow arbitrary characters

    if([string rangeOfCharacterFromSet:allowedCharacters.invertedSet].location == NSNotFound){

        return YES;

    }

    return NO;

}

答案 2 :(得分:2)

这是方式:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        // Only characters in the NSCharacterSet you choose will insertable.
        NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefgABCDEFG"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
        bool cond1 = [string isEqualToString:filtered];

     if (textField.text.length >= 10 && range.length == 0){
          return NO;
     }else{
          return (cond1);
     }

    }