12

我有两个文本字段,我想限制字符的数量和类型。我已经使用以下代码分别执行每个功能,但找不到在同一个功能中同时执行这两个功能的方法。

限制字符类型:

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

3 回答 3

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

已编辑

如果您想为第三个文本字段添加不同的条件,那么您可以这样做。
为第三个文本字段创建参考说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];
    }
}
于 2012-09-04T14:30:52.527 回答
3

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

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

}
于 2013-07-25T14:08:53.953 回答
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);
     }

    }
于 2012-09-04T14:29:46.960 回答