需要有关shouldChangeCharactersInRange的帮助

时间:2013-09-23 05:37:50

标签: ios objective-c uitextfield uitextfielddelegate

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@" "] || [string isEqualToString:@"@"] || [string isEqualToString:@"!"] || [string isEqualToString:@"#"] || [string isEqualToString:@"$"] || [string isEqualToString:@"%"] || [string isEqualToString:@"^"] || [string isEqualToString:@"&"] || [string isEqualToString:@"*"] || [string isEqualToString:@"("] || [string isEqualToString:@")"] || [string isEqualToString:@"+"] || [string isEqualToString:@"-"])
        return NO;
    return YES;
}

我正在使用此方法来限制所有这些字符。当用户从键盘输入文本时,它工作正常。如果用户粘贴文本或使用siri输入文本,则不会发生任何事情。

如何修复它。

4 个答案:

答案 0 :(得分:2)

当您通过键盘输入文本时, shouldChangeCharactersInRange 会为每个条目提供 string 参数中的单个字符。当您粘贴它或通过 siri 输入时,它会为您提供已添加的完整字符串或单词。在您的情况下,您将字符串作为单个字符进行比较,该字符在粘贴和siri情况下失败。我已经为您编写了代码片段。试试吧,告诉我它是否有效。它基本上从字符串中删除特殊字符。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string length] > 0)
    {
         NSCharacterSet *notAllowedChars = [NSCharacterSet characterSetWithCharactersInString:@"~!@#$%^&*(){}_+:\"<>?,./;'[]-="];
         NSString *resultString = [[string componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
         textField.text = [NSString stringWithFormat:@"%@%@", textField.text, resultString];
         return NO;
    }
    return YES;
}

答案 1 :(得分:1)

尝试自定义您的委托方法:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([string isEqualToString:@" "] || [string isEqualToString:@"@"] || [string isEqualToString:@"!"] || [string isEqualToString:@"#"] || [string isEqualToString:@"$"] || [string isEqualToString:@"%"] || [string isEqualToString:@"^"] || [string isEqualToString:@"&"] || [string isEqualToString:@"*"] || [string isEqualToString:@"("] || [string isEqualToString:@")"] || [string isEqualToString:@"+"] || [string isEqualToString:@"-"])
    {
        return NO;
    }
    else if([string length] > 1)
    {
        NSCharacterSet *notAllowedChars = [NSCharacterSet characterSetWithCharactersInString:@"@!!#$%^&*()+-@%"];
        NSString *resultString = [[string componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
        textField.text = resultString;
        return NO;
    }
    return YES;
}

答案 2 :(得分:0)

您当前的代码仅检查与单个字符的相等性。粘贴字符串可以包含多个字符。您可以使用字符集过滤字符串:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *invalidCharacters = [NSCharacterSet characterSetWithCharactersInString:@" @!#$%%^&*()+-"];
    if ([string rangeOfCharacterFromSet:invalidCharacters].location != NSNotFound) {
        NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        textField.text = [[newText componentsSeparatedByCharactersInSet:invalidCharacters] componentsJoinedByString:@""];

        return NO;
    } else {
        return YES;
    }
}

答案 3 :(得分:0)

#define Alpha_Numeric @“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 _。”

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

        NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:Alpha_Numeric] invertedSet];

        NSString *resStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];

        return [string isEqualToString:resStr];
    }