禁止表情输入,标点符号,中文,英文都能正常输入
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([self isInputRuleAndNumber:text] || [tex tisEqualToString:@""]){
return YES;
}else{
returnNO;
}
}
//textField输入的监听方法:过滤表情等
- (void)textFieldDidChange:(UITextField*)textField{
NSString*toBeString = textField.text;
NSString*lastString;
if(toBeString.length>0)
lastString=[toBeString substringFromIndex:toBeString.length-1];
if(![self isInputRuleAndNumber:toBeString]&&[self hasEmoji:lastString]) {
textField.text= [self disable_emoji:toBeString];
return;
}
}
- (BOOL)hasEmoji:(NSString*)str{
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", pattern];
BOOLisMatch = [predevaluateWithObject:str];
returnisMatch;
}
- (NSString*)disable_emoji:(NSString*)text{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
returnmodifiedString;
}
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
NSString *other = @"➋➌➍➎➏➐➑➒?,.。,、;:!~()+_-={}[]|?¥!@#$%^&*""<>:()/;“”‘’【】";
unsignedlonglen=str.length;
for(inti=0;i
{
unichara=[strcharacterAtIndex:i];
if(!((isalpha(a))
||(isalnum(a))
||((a=='_') || (a =='-'))
||((a >=0x4e00&& a <=0x9fa6))
||([otherrangeOfString:str].location!=NSNotFound)
))
returnNO;
}
return YES;
}
网友评论