注册

关于demo3.0表情,文字混合输入问题

在上一个2.0版本中,我有一天脑洞大开,突然想试试,聊天的输入内容是如何删除的。于是我输入了一些文字,发现删除时确实是一个一个删除的,接着测试,我在输入文字的同时,输入了表情,demo自带的表情。然后删除,发现也没问题。那么继续,我在输入文字时,夹杂了系统中得emoji表情。然后,哈哈哈,程序跪了。查看代码后,发现一个emoji表情占两个字符,而是不是emoji表情居然是通过是否包含在_defaultEmoji进行的判断,而_defaultEmoji只包含demo自己的30多个表情,果然demo就是demo,参考一下就好。造成奔溃的原因就是我们输入了不属于_defaultEmoji的表情,在进行删除时,没有按两个字符进行删除。
#pragma mark - DXFaceDelegate

- (void)selectedFacialView:(NSString *)str isDelete:(BOOL)isDelete
{
NSString *chatText = self.inputTextView.text;

NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:self.inputTextView.attributedText];

if (!isDelete && str.length > 0) {
NSRange range = [self.inputTextView selectedRange];
[attr insertAttributedString:[EaseEmotionEscape attStringFromTextForInputView:str] atIndex:range.location];
self.inputTextView.text = @"";
self.inputTextView.attributedText = attr;
// self.inputTextView.text = [NSString stringWithFormat:@"%@%@",chatText,str];
}
else {
if (chatText.length > 0) {
NSInteger length = 1;
if (chatText.length >= 2) {
NSString *subStr = [chatText substringFromIndex:chatText.length-2];
if ([_defaultEmoji containsObject:subStr]) {
length = 2;
}
}
self.inputTextView.attributedText = [self backspaceText:attr length:length];
}
}

[self textViewDidChange:self.inputTextView];
}
解决也很简单,我再判断表情时,同时使用了下面的方法。这个方法我加在了NSString的扩展中。
+ (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;
}
注意:下面的代码为更新版本,上面的代码还是太久远了,下面的也是网上找到的,感觉还比较新。
+ (void)load {

    VariationSelectors = [NSCharacterSet characterSetWithRange:NSMakeRange(0xFE00, 16)];

}




- (BOOL)isEmoji {

    if ([self rangeOfCharacterFromSet: VariationSelectors].location != NSNotFound) {

        return YES;

    }

    

    const unichar high = [self characterAtIndex: 0];

    

    // Surrogate pair (U+1D000-1F9FF)

    if (0xD800 <= high && high <= 0xDBFF) {

        const unichar low = [self characterAtIndex: 1];

        const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;

        

        return (0x1D000 <= codepoint && codepoint <= 0x1F9FF);

        

        // Not surrogate pair (U+2100-27BF)

    } else {

        return (0x2100 <= high && high <= 0x27BF);

    }

}

1 个评论

刚测了下,确实有这个问题。已经录入知识点了,刚赞赏了,快去提现看看。

要回复文章请先登录注册