字符的Xcode的UITextField限制类型(Xcode UITextField limit t

2019-10-30 11:09发布

在Xcode中,我怎么能限制的UITextField与“ABCD1234567”字,前4个字符是没有特殊字符的字母和其他国家必须有6或7号。 请帮我解决这个问题。 谢谢提前。

Answer 1:

定义以下变量

#define NUMBERS @"0123456789"
#define ALPHABATES @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

现在,在文本框的委托功能,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    NSString *filtered;
    //for maximum characters
    if (textField.text.length >= 11 && range.length == 0)
        return NO;

    if(textField.text.length<11)
    {
        NSString *cStr=[NSString stringWithFormat:@"%@%@",textField.text,string];
        if([cStr length]<11)
        {
            if(range.location<4)
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHABATES] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            else
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
        }
        else
        {
            int iPrevLength,iNextLength;
            iPrevLength=[textField.text length];
            iNextLength=11;
            int iLengthDiff=iNextLength-iPrevLength;
            string=[string substringToIndex:iLengthDiff];
            if(range.location<4)
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHABATES] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            else
            {

                cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
                filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
                return [string isEqualToString:filtered];
            }
            if([string isEqualToString:filtered])
            textField.text=[NSString stringWithFormat:@"%@%@",textField.text,string];
            return NO;
        }
    }

    return YES;
}

确保为ALPHABATES有大写字母,因此将只盖字母如A,BC不是,B,C的工作....

它首先接受ALPHABATES然后6-7数字字母的4字母。

我希望它会为你工作。

让我知道。



Answer 2:

//this method is delegate method of UITextField
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    int strLen = [textField.text length];
    BOOL invalidChar=FALSE;


if (strLen<=11)
{
    for (int i=0,j=1;i<strLen;i++,j++)
    {
        char strChar = [textField.text characterAtIndex:i];

        if (j<=4)
        {
            if (strChar<65 || strChar>90)
            {
                invalidChar=TRUE;
                break;
            }
        }

        if (j>=5)
        {
            if (strChar<48 || strChar>57)
            {
                invalidChar=TRUE;
                break;
            }
        }
    }
}
else
{
    [txtString becomeFirstResponder];
}

if (invalidChar)
{
    [txtString becomeFirstResponder];
}
}


文章来源: Xcode UITextField limit type of characters