Preface
This article mainly introduces the useUITextField
Limits can only enter Chinese, English, and numbers, we can useNSPredicate
Regular expressions can be filtered, let’s take a look at the detailed steps and methods below
First set the proxy of UItextField
Implement the following method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) { return YES; } return NO; }
Then add events because the input method associative words will not passtextField:shouldChangeCharactersInRange:replacementString
:Method
[textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
accomplish:
- (void)textFieldChanged:(UITextField *)textField { NSString *toBeString = ; NSString *lastString; if(>0) lastString=[toBeString substringFromIndex:-1]; if (![self isInputRuleAndNumber:toBeString]&&[self hasEmoji:lastString]) { = [self disable_emoji:toBeString]; return; } NSString *lang = [[textField textInputMode] primaryLanguage]; if([lang isEqualToString:@"zh-Hans"]) { UITextRange *selectedRange = [textField markedTextRange]; UITextPosition *position = [textField positionFromPosition: offset:0]; if(!position) { NSString *getStr = [self getSubString:toBeString]; if(getStr && > 0) { = getStr; } } } else{ NSString *getStr = [self getSubString:toBeString]; if(getStr && > 0) { = getStr; } } }
Next, implement the restrictions:
In the pattern, enter the passed character that needs to be verified
Lowercase a-z
Capital A-Z
Chinese characters\u4E00-\u9FA5
Number\u0030-\u0039
- (BOOL)isInputRuleAndNumber:(NSString *)str { NSString *pattern = @"[a-zA-Z\u4E00-\u9FA5\\u0030-\\u0039]"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:str]; return isMatch; }
Implement to determine whether it is Emoji
- (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 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:str]; return isMatch; }
Character limit
#define kMaxLength 20; -(NSString *)getSubString:(NSString*)string { NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSData* data = [string dataUsingEncoding:encoding]; NSInteger length = [data length]; if (length > kMaxLength) { NSData *data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength)]; NSString *content = [[NSString alloc] initWithData:data1 encoding:encoding]; if (!content || == 0) { data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength - 1)]; content = [[NSString alloc] initWithData:data1 encoding:encoding]; } return content; } return nil; }
Summarize
The above is the entire content of this article. I hope it will help you study or work. If you have any questions, you can leave a message to communicate.