iOStextField/textView在输入时限制emoji表情的输入

又遇到输入框输入表情的情况了,之前写了一篇文章“UITextView/UITextField检测并过滤Emoji表情符号”http://www.jianshu.com/p/90d68e7e5d53,但是总觉得那两种方式都各有弊端,这次又遇到之后,仔细考虑了下之后,想到了用两种方式组合在一起使用,测试结果暂时没什么问题,在输入时就限制了emoji表情输入,完全符合需求。在此贴出代码,如果有什么问题,欢迎指正!

@interface JQTestViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, assign) NSInteger textLocation;//这里声明一个全局属性,用来记录输入位置
@end

- (void)viewDidLoad {
[super viewDidLoad];
self.textField.delegate = self;
[self.textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];//注意:textFied没有textFieldDidChanged代理方法,但是有UITextFieldTextDidChangeNotification通知,这里添加通知方法,textView有textFieldDidChanged代理方法,下面用法一样
}

#pragma mark-- UITextFieldDelegate
//在输入时,调用下面那个方法来判断输入的字符串是否含有表情
- (void)textFieldDidChanged:(UITextField *)textField
{
    if (textField.text.length > 20) {
        textField.text = [textField.text substringToIndex:20];
        [self showMessage:@"不可超过20字!"];
    }else {
        if (self.textLocation == -1) {
            NSLog(@"输入不含emoji表情");
        }else {
            NSLog(@"输入含emoji表情");
            //截取emoji表情前
            textField.text = [textField.text substringToIndex:self.textLocation];
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"location-->>%lu",(unsigned long)range.location);
    NSLog(@"replacementString-->>%@",string);
    //禁止输入emoji表情
    if ([self stringContainsEmoji:string]) {
        self.textLocation = range.location;
    }else {
        self.textLocation = -1;
    }
    return YES;
}

//表情符号的判断
- (BOOL)stringContainsEmoji:(NSString *)string {
    
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
}

推荐阅读更多精彩内容