键入一些字符后如何限制用户键入文本字段

时间:2012-05-15 10:37:16

标签: iphone objective-c ios5

我在我的应用程序中使用textfield,我想限制用户在文本字段中只键入15个字符。之后,他/她应该无法在文本字段中输入

如何设置此类功能?

5 个答案:

答案 0 :(得分:1)

对此有一点诀窍,你需要先计算新字符串的含义,然后才能测试是否允许或拒绝更改

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

    if ([newString length] > 15) {
        return FALSE;
    } else {
        return TRUE;
    }
}

答案 1 :(得分:1)

//它的大代码但对我来说运作正常

//在此代码中放置您的文本字段名称而不是YourTextFieldName

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

     {

      if (textField==YourTextFieldName)
      {

        NSString *text = nil;
        int MAX_LENGTH = 12;

        text = YourTextFieldName.text;
        if ([text length] <= 7)
         {
            NSString *separator = @"-";
            int seperatorInterval = 3;
            NSString *originalString = [textField.text         stringByReplacingOccurrencesOfString:separator withString:@""];

            if (![originalString isEqualToString:@""] && ![string isEqualToString:@""])
            {

                NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFileName.text length] - 1];
                int modulus = [originalString length] % seperatorInterval;

                if (![lastChar isEqualToString:separator] && modulus == 0)
                {
                    YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator];
                }
            }
        }
        if ([text length] > 7)
        {
            NSString *separator = @"-";
            int seperatorInterval = 6;
            NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:@""];

            if (![originalString isEqualToString:@""] && ![string isEqualToString:@""])
            {

                NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFieldName.text length] - 1];
                int modulus = [originalString length] % seperatorInterval;

                if (![lastChar isEqualToString:separator] && modulus == 0)
                {

                    YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator];
                }
            }
        }

           NSUInteger newLength = [textField.text length] + [string length] - range.length;
         return (newLength > MAX_LENGTH) ? NO : YES;
     }
    return YES;



     }

答案 2 :(得分:0)

您可以检查是否输入和计算字符

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range      replacementString:(NSString *)string
    {
    if (range.length > 15) {
        // delete
    }
    else
    {
        // add
    }
}

答案 3 :(得分:0)

使用方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (([textField.text length] - range.length) == 15) {
        return NO;
    }
    return YES;
}
希望它有所帮助。快乐的编码:)

答案 4 :(得分:-1)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= 15)
    {
        return NO; //return NO to not change text
    }

return YES;

}