(转)UITextField只限中文、英文、数字输入和限制字符个
原文链接:http://www.jianshu.com/p/ea6b8c026f6b
写在前面
一般情况下一个字母、符号、数字相当于一个字符,一个汉字相当于两个字符,一个emoji表情相当于两个或更多字符。本文要实现的是在UITextField只允许输入一定字符数的中文、英文和数字,不允许有其他符号输入(包括emoji表情)。
开始
粮草未行, Demo先上
- 添加代理及文本输入变化通知UIControlEventEditingChanged
self.textField.delegate = self;
[self.textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
2.实现以下代理及通知
-
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self isInputRuleNotBlank:string] || [string isEqualToString:@""]) {//当输入符合规则和退格键时允许改变输入框
return YES;
} else {
return NO;
}
} -
(void)textFieldChanged:(UITextField *)textField {
NSString *toBeString = textField.text;
if (![toBeString isTeamNameAndBlank]) {//【注意2】处理在系统输入法简体拼音下可选择表情的情况
textField.text = [self disable_emoji:toBeString];
return;
}NSString *lang = [[textField textInputMode] primaryLanguage]; // 获取当前键盘输入模式
NSLog(@"%@",lang);
if([lang isEqualToString:@"zh-Hans"]) { //简体中文输入,第三方输入法(搜狗)所有模式下都会显示“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;
}
}
}
【注意1】:在中文输入时候选区选中表情时会绕过代理- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string ,这是需要判断最后选中字符是否符合限制规则
【注意2】:处理在系统输入法简体拼音下可选择表情的情况
3.其他方法
/**
- 字母、数字、中文正则判断(不包括空格)
*/
- (BOOL)isInputRuleNotBlank:(NSString )str {
NSString pattern = @"^[a-zA-Z\u4E00-\u9FA5\d]$";
NSPredicate pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}
/
- 字母、数字、中文正则判断(包括空格)【注意3】
*/
-
(BOOL)isInputRuleAndBlank:(NSString *)str {
NSString pattern = @"^[a-zA-Z\u4E00-\u9FA5\d\s]$";
NSPredicate pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:str];
return isMatch;
}
/*
- 获得 kMaxLength长度的字符
/
-(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];//【注意4】:当截取kMaxLength长度字符时把中文字符截断返回的content会是nil
if (!content || content.length == 0) {
data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength - 1)];
content = [[NSString alloc] initWithData:data1 encoding:encoding];
}
return content;
}
return nil;
}
/**
- 过滤字符串中的emoji
*/
- (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:@""];
return modifiedString;
}