JoshPell

人生只有必然,没有偶然

欢迎来到我的个人网站~


emoji表情作为单字符长度的处理

我们知道emoji表情是一些特殊格式的字符串,因此我们在计算其长度的时候直接用“xxx.length”即可,但有时候产品经理会要求你把所有的emoji表情都当做一个字符处理,别问我怎么知道的😭😭😭,这里写下来希望大家不要跟我一样到处跳坑

还是直接上代码

1.获取emoji个数(我这里写到了NSString的类方法中)

+ (NSInteger)emojiCountInString:(NSString *)string
{
    __block NSInteger num = 0;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull 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 <= 0x1f918)
				{
					num += 1;
				}
			}
		}
		else if (substring.length > 1)
		{
			const unichar ls = [substring characterAtIndex:1];
			if (ls == 0x20e3 || ls == 0xFE0F || ls == 0xd83c)
			{
				num += 1;
			}
		}
	    else{
		// non surrogate
			if (0x2100 <= hs && hs <= 0x27ff)
			{
			if (0x278b <= hs && 0x2792 >= hs)
			{
				//不为emoji
			}
			else
			{
				num += 1;
			}
		}
		else if (0x2B05 <= hs && hs <= 0x2b07)
		{
			num += 1;
		}
		else if (0x2934 <= hs && hs <= 0x2935)
		{
			num += 1;
		}
		else if (0x3297 <= hs && hs <= 0x3299)
		{
			num += 1;
		}
		else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0xd83e)
		{
			num += 1;
		}
	}
	}];
    return num;
}

2.字符串去掉emoji

+ (NSString *)getStringWithoutEmoji:(NSString *)string
{
    __block NSString *tempStr = string;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                // surrogate pair
                                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 <= 0x1f918)
                                        {

                                            tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                        }
                                    }
                                }
                                else if (substring.length > 1)
                                {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3 || ls == 0xFE0F || ls == 0xd83c)
                                    {
                                        tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                    }
                                }
                                else
                                {
                                    // non surrogate
                                    if (0x2100 <= hs && hs <= 0x27ff)
                                    {
                                        if (0x278b <= hs && 0x2792 >= hs)
                                        {
                                            //不为emoji
                                        }
                                        else
                                        {
                                            tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                        }
                                    }
                                    else if (0x2B05 <= hs && hs <= 0x2b07)
                                    {
                                        tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                    }
                                    else if (0x2934 <= hs && hs <= 0x2935)
                                    {
                                        tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                    }
                                    else if (0x3297 <= hs && hs <= 0x3299)
                                    {
                                        tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                    }
                                    else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0xd83e)
                                    {
                                        tempStr = [tempStr stringByReplacingOccurrencesOfString:substring withString:@""];
                                    }
                                }
                            }];
    return tempStr;
}

3.获取把emoji表情当成单个字符长度处理后的字符串长度

-(NSInteger)lengthWithEmojiString{
    NSInteger emojiLen = [NSString emojiCountInString:self];
    NSString * normalStr = [NSString getStringWithoutEmoji:self];
    NSInteger strLengh = normalStr.length+emojiLen;
    return strLengh;
}

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

Powered by 谭健,分享从这里开始,精彩与您同在


正在加载中……