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

4 回答 4

2

当您通过键盘输入文本时,shouldChangeCharactersInRange会在每个条目的字符串参数中为您提供单个字符。当您粘贴它或通过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;
}
于 2013-09-23T05:51:00.320 回答
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;
}
于 2013-09-23T06:19:33.247 回答
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];
    }
于 2013-09-23T06:44:39.020 回答
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;
    }
}
于 2013-09-23T05:52:53.637 回答