Use UITextField to restrict entry to Chinese English and numeric methods only
- 2020-11-26 19:00:43
- OfStack
preface
This article focuses on usage
UITextField
Limits can only be entered in Chinese, English, Numbers that we can use
NSPredicate
Regular expressions can be filtered, so let's look at the step-by-step method
First set up the UItextField agent
The following methods are implemented:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
return YES;
}
return NO;
}
Then add the event because the input method is selected to associate the word. It doesn't go through
textField:shouldChangeCharactersInRange:replacementString
Method: the
[textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
Implementation:
- (void)textFieldChanged:(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;
}
NSString *lang = [[textField textInputMode] primaryLanguage];
if([lang isEqualToString:@"zh-Hans"]) {
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if(!position) {
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text = getStr;
}
}
} else{
NSString *getStr = [self getSubString:toBeString];
if(getStr && getStr.length > 0) {
textField.text= getStr;
}
}
}
Again, to implement the restrictions:
In pattern, enter the passing characters that need to be validated
Lowercase a - z
Capital A - Z
Chinese characters \ u4E00 - \ u9FA5
Digital \ 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;
}
The implementation determines if 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 || content.length == 0) {
data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength - 1)];
content = [[NSString alloc] initWithData:data1 encoding:encoding];
}
return content;
}
return nil;
}
conclusion
The above is the whole content of this article, I hope to bring 1 definite help to your study or work, if you have any questions, you can leave a message to communicate.