检查textFields是否输入了字符

时间:2014-09-18 14:41:59

标签: ios nsstring textview uitextfield character

我使用该函数来检查字符用户是否已键入。 它有效。 问题是如果用户进行复制粘贴并且文本包含一些受限制的字符 - 文本不会出现。

我从shouldChangeCharactersInRange启动它:( NSRange)范围replacementString:(NSString *)string

如何重写此函数以返回包含所有允许字符但不受限制的字符串?

 + (BOOL)isValidCharacters:(NSDictionary *)parameters forString:(NSString *)string
{
//выбираем разрешенные символы
NSMutableCharacterSet *allowedCharacters = [[NSMutableCharacterSet alloc]init];

if (parameters[@"whitespaceCharacterSet"])//пробел
[allowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];

if (parameters[@"decimalDigitCharacterSet"]) //цифры
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];

if (parameters[@"alphanumericCharacterSet"]) //буквы цифры
[allowedCharacters formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];

if (parameters[@"symbolCharacterSet"]) //символы
[allowedCharacters formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];

if (parameters[@"letterCharacterSet"])//буквы
    [allowedCharacters formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];

if (parameters[@"arbitraryCharacters"]) //набор
[allowedCharacters addCharactersInString:@":.,;_!()/\n"];

if (parameters[@"email"]) //email
    [allowedCharacters addCharactersInString:@"._@"];

if([string rangeOfCharacterFromSet:allowedCharacters.invertedSet].location == NSNotFound)
{

    return YES;
}

return NO;

0 个答案:

没有答案