- 最近在做一个注册页面,按需求,要过滤一些特殊字符、Emoje表情 (只允许输入 中文、英文、数字还有空格)来创建用户昵称。
-
发现九宫格输入有个细微的问题: 【输入中文九宫格的预设拼音打不出来】。
经过一顿查找发现: 中文九宫格输入值为特殊字符。
既然找到问题所在了,那么就好解决了,在判断的时候要考虑把九宫格的特殊字符也加进去。
/**
* 判断 字母、数字、中文
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
NSString *other = @"➋➌➍➎➏➐➑➒"; //九宫格的输入值
unsigned long len=str.length;
for(int i=0;i<len;i++)
{
unichar a=[str characterAtIndex:i];
if(!((isalpha(a))
||(isalnum(a))
// ||((a=='_') || (a == '-')) //判断是否允许下划线,昵称可能会用上
||((a==' ')) //判断是否允许控制
||((a >= 0x4e00 && a <= 0x9fa6))
||([other rangeOfString:str].location != NSNotFound)
))
return NO;
}
return YES;
}
整个过程如下:
- 创建TextFiled、设置代理和值改变的监听
_nickNameTextField = ({
UITextField *textFiled =[[UITextField alloc]initWithFrame:CGRectMake(self.view.center.x - 240 * 0.5, 200, 240, 22)];
textFiled.placeholder = @"只允许输入中文、英文、数字";
//设置输入框的代理
textFiled.delegate = self;
//创建输入框的通知
[textFiled addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:textFiled];
textFiled;
});
- 在TextFiled代理中进行判断是否满足规则允许输入
#pragma mark ============== TextFieldDelegate ==========
//判断是否满足规则 改变输入框
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
return YES;
}
return NO;
}
- (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;
}
}
}
#pragma mark - privately Method
/**
* 判断 字母、数字、中文
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
NSString *other = @"➋➌➍➎➏➐➑➒"; //九宫格的输入值
unsigned long len=str.length;
for(int i=0;i<len;i++)
{
unichar a=[str characterAtIndex:i];
if(!((isalpha(a))
||(isalnum(a))
// ||((a=='_') || (a == '-')) //判断是否允许下划线,昵称可能会用上
||((a==' ')) //判断是否允许控制
||((a >= 0x4e00 && a <= 0x9fa6))
||([other rangeOfString:str].location != NSNotFound)
))
return NO;
}
return YES;
}
/**
* 获得 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];//注意:当截取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
*/
- (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;
}
- (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;
}
代码不多,不是很难理解,这次懒得解释,还有一个值得注意的就是 修改 字符最大值,使用了一个宏进行管理。 睡觉去~ 0 0
#define kMaxLength 16
参考链接
http://blog.csdn.net/wodeni512517/article/details/51941606