UITextView,UITextField实时监测-过滤

关键字:表情符号、正则表达式、汉字、检测、过滤、搜狗换行

每一次的文章都是产品妹子无脑的需求产生的,这次也不例外。

狗狗说了,name(UITextField)只能是小括号、逗号、中划线以及字母数字汉字,不能有特殊符号;

狗狗还说了,remark(UITextView)不能有表情符号,以及其他的一些符号;

UITextField监测-过滤

首先需要添加一个事件[textfieldName addTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventAllEditingEvents];

这样再加上本来有的事件,可以监测2个

#pragma mark - UITextFieldDelegate

-(void)textFieldDidChange:(UITextField*)textField{

[[***NameRules sharedManager] textFieldDidChange:textField];

}

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

return[[***NameRules sharedManager] textField:textFieldshouldChangeCharactersInRange:rangereplacementString:string];

}

然后是封装的事件处理函数:

@interface***NameRules()

@property(nonatomic,assign)BOOLisSouGouNewLine;

@property(nonatomic,strong)UITextField*preTextField;

@end

@implementation***NameRules

+(***NameRules*) sharedManager{

static ***NameRules*sharedNameRules =nil;

staticdispatch_once_tpreDicate;

dispatch_once(&preDicate, ^{

sharedNameRules = [[selfalloc]init];

});

returnsharedNameRules;

}

-(NSString*)textFieldDidChange:(UITextField*)textField

{

UITextRange*selectedRange = [textFieldmarkedTextRange];

NSString* newText = [textFieldtextInRange:selectedRange];

if(newText.length>0) {//输入拼音时候

return@"";

}

if(![***Rules validateName:textField.text])

{

[WToast showWithText:kNameDescExisted];

textField.text= [***Rules repairName:textField.text];;

}

return@"";

}

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

if([stringisEqualToString:@"\n"] )

{

[textFieldresignFirstResponder];

returnNO;

}

if(self.isSouGouNewLine&& string.length==0) {

self.isSouGouNewLine=NO;

self.preTextField=nil;

returnNO;

}

elseif(self.isSouGouNewLine==YES){

self.preTextField=nil;

self.isSouGouNewLine=NO;

}

if([stringisEqualToString:@"\r\r"]) {

self.isSouGouNewLine=YES;

self.preTextField= textField;

}

returnYES;

}

@end

上面代码主要就是在shouldChangeCharactersInRange,里面直接过滤不要的(产品狗说不要),比如回车,比如搜狗的那个换行;然后textFieldDidChange里面直接监测过滤,注意拼音输入的时候先不要过滤。

另外那个搜狗换行,为什么是\r\r然后一个退格呢,处理的比较恶心,有人有更好的办法吗?

UITextView跟UITextField没什么大的区别,他对应 是下面2个事件,都不需要用户自己加监听:

-(NSString*)textViewDidChange:(UITextView*)textView;

- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;


最后说说那些过滤函数:

1.表情符号检测和过滤

+(BOOL)isContainsEmoji:(NSString*)string{

__blockBOOLisEomji =NO;

[stringenumerateSubstringsInRange:NSMakeRange(0, [stringlength])options:NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString*substring,NSRangesubstringRange,NSRangeenclosingRange,BOOL*stop) {

constunicharhs = [substringcharacterAtIndex:0];

// surrogate pair

if(0xd800<= hs && hs <=0xdbff) {

if(substring.length>1) {

constunicharls = [substringcharacterAtIndex:1];

constintuc = ((hs -0xd800) *0x400) + (ls -0xdc00) +0x10000;

if(0x1d000<= uc && uc <=0x1f77f) {

isEomji =YES;

}

}

}else{

if(hs>=0x278b&& hs <=0x2792) {

//九宫格输入这个是中文输入法中输入字符,不能算符号。

;

}

// non surrogate

elseif(0x2100<= hs && hs <=0x27ff&& hs !=0x263b) {

isEomji =YES;

}elseif(0x2B05<= hs && hs <=0x2b07) {

isEomji =YES;

}elseif(0x2934<= hs && hs <=0x2935) {

isEomji =YES;

}elseif(0x3297<= hs && hs <=0x3299) {

isEomji =YES;

}elseif(hs ==0xa9|| hs ==0xae|| hs ==0x303d|| hs ==0x3030|| hs ==0x2b55|| hs ==0x2b1c|| hs ==0x2b1b|| hs ==0x2b50|| hs ==0x231a) {

isEomji =YES;

}

if(!isEomji && substring.length>1) {

constunicharls = [substringcharacterAtIndex:1];

if(ls ==0x20e3) {

isEomji =YES;

}

}

}

}];

returnisEomji;

}

+(NSString*)removeEmoji:(NSString*)string{

[***RulessharedManager].noEomji=@"";

[stringenumerateSubstringsInRange:NSMakeRange(0, [stringlength])options:NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString*substring,NSRangesubstringRange,NSRangeenclosingRange,BOOL*stop) {

BOOLisEomji =NO;

constunicharhs = [substringcharacterAtIndex:0];

// surrogate pair

if(0xd800<= hs && hs <=0xdbff) {

if(substring.length>1) {

constunicharls = [substringcharacterAtIndex:1];

constintuc = ((hs -0xd800) *0x400) + (ls -0xdc00) +0x10000;

if(0x1d000<= uc && uc <=0x1f77f) {

isEomji =YES;

}

}

}else

{

if(hs>=0x278b&& hs <=0x2792) {

//九宫格输入这个是中文输入法中输入字符,不能算符号。

;

}

// non surrogate

elseif(0x2100<= hs && hs <=0x27ff&& hs !=0x263b) {

isEomji =YES;

}elseif(0x2B05<= hs && hs <=0x2b07) {

isEomji =YES;

}elseif(0x2934<= hs && hs <=0x2935) {

isEomji =YES;

}elseif(0x3297<= hs && hs <=0x3299) {

isEomji =YES;

}elseif(hs ==0xa9|| hs ==0xae|| hs ==0x303d|| hs ==0x3030|| hs ==0x2b55|| hs ==0x2b1c|| hs ==0x2b1b|| hs ==0x2b50|| hs ==0x231a) {

isEomji =YES;

}

if(!isEomji && substring.length>1) {

constunicharls = [substringcharacterAtIndex:1];

if(ls ==0x20e3) {

isEomji =YES;

}

}

}

if(!isEomji) {

[***RulessharedManager].noEomji= [NSStringstringWithFormat:@"%@%@",[***RulessharedManager].noEomji,substring];

}

}];

return[***RulessharedManager].noEomji;

}

2.名称、备注判断-过滤

+ (BOOL) validateName:(NSString*)name

{

if(name.length<=0) {

returnYES;

}

if([selfisContainsEmoji:name]) {

returnNO;

}

NSString*nameRegex =@"[0-9a-zA-Z\u4e00-\u9fa5\u278b-\u2792\\)\\(\\\\.-]+";

NSPredicate*passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",nameRegex];

return[passWordPredicateevaluateWithObject:name];

}

+(BOOL)validateRemark:(NSString*)sText{

if(sText.length<=0) {

returnYES;

}

if([selfisContainsEmoji:sText]) {

returnNO;

}

//

NSString*nameRegex =@"[0-9a-zA-Z\u4e00-\u9fa5\u278b-\u2792\\.\\*\\)\\(\\+\\$\\[\\?\\\\\\^\\{\\|\\]\\}\\-%%%@\r\t\'\",。‘、-【】·!_——=:;;<>《》‘’“”!#~]+";

NSPredicate*passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",nameRegex];

return[passWordPredicateevaluateWithObject:sText];

}

+(NSString*)removeOtherChar:(NSString*)string WithChars:(NSString*)rule{

[***RulessharedManager].noEomji=@"";

[stringenumerateSubstringsInRange:NSMakeRange(0, [stringlength])options:NSStringEnumerationByComposedCharacterSequencesusingBlock:^(NSString*substring,NSRangesubstringRange,NSRangeenclosingRange,BOOL*stop) {

NSPredicate*passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",rule];

BOOLisValidate = [passWordPredicateevaluateWithObject:substring];

if(isValidate) {

[***RulessharedManager].noEomji= [NSStringstringWithFormat:@"%@%@",[***RulessharedManager].noEomji,substring];

}

}];

return[***RulessharedManager].noEomji;

}

你可能感兴趣的:(UITextView,UITextField实时监测-过滤)