当前位置:   article > 正文

表情符号emojiUTF-8编码、Unicode、HTML显示_emoji utf8编码

emoji utf8编码

一、把含有表情的字符串编码 ---- UTF-8

  1. - (NSString *)emojiEncode{
  2. NSString *uniStr = [NSString stringWithUTF8String:[self UTF8String]];
  3. NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
  4. NSString *emojiText = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding];
  5. return emojiText;
  6. }

二、把请求到的含有表情字符串数据解码,解码后直接在UILabel上显示 ---- UTF-8

  1. - (NSString *)emojiDecode{
  2. const char *jsonString = [self UTF8String];
  3. NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
  4. NSString *emojiText = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
  5. return emojiText;
  6. }

注意:这样做微信端不能显示表情,也不能显示微信端上传的表情。

三、使用网页形式的表情格式(&#数字;)显示表情

//遍历含有表情的字符串,把表情替换成HTML格式的
//把UITextView或者UItextField里面text直接拿出来进行表情格式化
//把格式化之后的数据上传服务器

  1. -(NSString *)replaceEmoji:(NSString *)text
  2. {
  3. //过滤表情正则表达式
  4. 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];
  5. // 对text字符串进行匹配
  6. NSArray *matches = [regex matchesInString:text
  7. options:0
  8. range:NSMakeRange(0, text.length)];
  9. // 遍历匹配后的每一条记录
  10. NSString *result = text;
  11. for (NSTextCheckingResult *match in matches) {
  12. NSRange range = [match range];
  13. NSString *target = [text substringWithRange:range];
  14. NSLog(@"%@", target);
  15. //把表情转换成HTML格式的
  16. NSString *emojiS = [target emojiUnicodeToHTML];
  17. //替换回原来的位置
  18. result = [result stringByReplacingOccurrencesOfString:target withString:emojiS];
  19. }
  20. return result;
  21. }

//把表情转成网页能显示型的HTML格式(&#数字;)微信直接显示

  1. #define MULITTHREEBYTEUTF16TOUNICODE(x,y) (((((x ^ 0xD800) << 2) | ((y ^ 0xDC00) >> 8)) << 8) | ((y ^ 0xDC00) & 0xFF)) + 0x10000
  2. - (NSString *)emojiUnicodeToHTML{
  3. NSString *hexstr = @"";
  4. if ([self length] >= 2) {
  5. for (int i = 0; i < [self length] / 2 && ([self length] % 2 == 0) ; i++)
  6. {
  7. // three bytes
  8. if (([self characterAtIndex:i*2] & 0xFF00) == 0 ) {
  9. hexstr = [hexstr stringByAppendingFormat:@"Ox%1X 0x%1X",[self characterAtIndex:i*2],[self characterAtIndex:i*2+1]];
  10. }
  11. else
  12. {// four bytes
  13. hexstr = [hexstr stringByAppendingFormat:@"%1X ",MULITTHREEBYTEUTF16TOUNICODE([self characterAtIndex:i*2],[self characterAtIndex:i*2+1])];
  14. }
  15. }
  16. NSLog(@"(unicode) [%@]",hexstr);
  17. }
  18. else
  19. {
  20. NSLog(@"(unicode) U+%1X",[self characterAtIndex:0]);
  21. }
  22. NSString *html = [NSString stringWithFormat:@"%lu",strtoul([hexstr UTF8String],0,16)];
  23. return [NSString stringWithFormat:@"&#%@;",html];
  24. }

//解析网页形式的表情格式在UILabel上显示
//把微信端表情转成Unicode编码(如果能支持unicode 字符串 那么直接获取U+1F604 输出即可,如果不支持,那么获取png图片显示即可,下面的convertSimpleUnicodeStr方法获取)
//把微信端的HTML形式的表情转换成iOS端显示用的Unicode格式

匹配HTML格式表情正则.png

  1. - (NSString *)digitalEmojiToUnicode:(NSString *)string
  2. {
  3. //把微信端上传的表情处理成iOS字符串
  4. NSString *text = [string htmlToString];
  5. //匹配HTML格式表情正则
  6. NSString *prefix = @"[^&#]*\\;";
  7. NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:prefix
  8. options:NSRegularExpressionCaseInsensitive
  9. error:nil];
  10. // 对text字符串进行匹配
  11. NSArray *matches = [regular matchesInString:text
  12. options:0
  13. range:NSMakeRange(0, text.length)];
  14. // 遍历匹配后的每一条记录
  15. NSString *result = text;
  16. for (NSTextCheckingResult *match in matches) {
  17. NSRange range = [match range];
  18. NSString *target = [text substringWithRange:range];
  19. NSLog(@"%@", target);
  20. //把HTML格式的表情转换为Unicode格式的
  21. NSString *emojiS = [target emojiHTMLToUnicode];
  22. //iOS 端直接支持unicode字符 (有一些高级表情会失败,返回nil)
  23. NSString *convertUnicode = [emojiS convertSimpleUnicodeStr];
  24. if (convertUnicode.length ==0) {
  25. convertUnicode = @"❓";(高级表情显示不出来)
  26. }
  27. //把表情替换回原来的位置,然后就能直接用UILabel显示表情了
  28. result = [result stringByReplacingOccurrencesOfString:[@"&#" stringByAppendingString:target] withString:convertUnicode];
  29. }
  30. return result;
  31. }

注意:上面代码里面的正则表达式不对,符号被简书改了,看图片。

  1. - (NSString *)emojiHTMLToUnicode{
  2. NSString *result = [self stringByReplacingOccurrencesOfString:@"&#" withString:@""];
  3. result = [result stringByReplacingOccurrencesOfString:@";" withString:@""];
  4. NSString *hexString = [NSString stringWithFormat:@"U+%@",[[NSString alloc] initWithFormat:@"%1X",[result intValue]]];
  5. return hexString;
  6. }

//这段函数作用是将 U+1F591 转换为 \U0001F591
//如果为 U+2500 那么就是补全为 \U00002500
//返回的sendStr就是可以直接在UILabel上显示的表情

  1. - (NSString *)convertSimpleUnicodeStr{
  2. NSString *strUrl = [self stringByReplacingOccurrencesOfString:@"U+" withString:@""];
  3. unsigned long unicodeIntValue= strtoul([strUrl UTF8String],0,16);
  4. // UTF32Char inputChar = unicodeIntValue ;// 变成utf32
  5. unsigned long inputChar = unicodeIntValue ;// 变成utf32
  6. // inputChar = NSSwapHostIntToLittle(inputChar); // 转换成Little 如果需要
  7. inputChar = NSSwapHostLongToLittle(inputChar); // 转换成Little 如果需要
  8. NSString *sendStr = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
  9. NSLog(@"%@",sendStr);
  10. return sendStr;

}

//网页内容转String

  1. - (NSString *)htmlToString{
  2. NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
  3. if (data.length > 0) {
  4. return [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInteger:NSUTF8StringEncoding]} documentAttributes:nil error:nil].string;
  5. }
  6. return @"";
  7. }

注意:这个方法是把网页格式的数据变成字符串

慢慢来,一步一个巴掌印。。。。。


链接:https://www.jianshu.com/p/8436bdd30769
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/253339
推荐阅读
相关标签
  

闽ICP备14008679号