赞
踩
一、把含有表情的字符串编码 ---- UTF-8
- - (NSString *)emojiEncode{
- NSString *uniStr = [NSString stringWithUTF8String:[self UTF8String]];
- NSData *uniData = [uniStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
- NSString *emojiText = [[NSString alloc] initWithData:uniData encoding:NSUTF8StringEncoding];
- return emojiText;
- }
二、把请求到的含有表情字符串数据解码,解码后直接在UILabel上显示 ---- UTF-8
- - (NSString *)emojiDecode{
- const char *jsonString = [self UTF8String];
- NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)];
- NSString *emojiText = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
- return emojiText;
- }
注意:这样做微信端不能显示表情,也不能显示微信端上传的表情。
三、使用网页形式的表情格式(&#数字;)显示表情
//遍历含有表情的字符串,把表情替换成HTML格式的
//把UITextView或者UItextField里面text直接拿出来进行表情格式化
//把格式化之后的数据上传服务器
- -(NSString *)replaceEmoji:(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];
- // 对text字符串进行匹配
- NSArray *matches = [regex matchesInString:text
- options:0
- range:NSMakeRange(0, text.length)];
- // 遍历匹配后的每一条记录
- NSString *result = text;
- for (NSTextCheckingResult *match in matches) {
- NSRange range = [match range];
- NSString *target = [text substringWithRange:range];
- NSLog(@"%@", target);
- //把表情转换成HTML格式的
- NSString *emojiS = [target emojiUnicodeToHTML];
- //替换回原来的位置
- result = [result stringByReplacingOccurrencesOfString:target withString:emojiS];
- }
- return result;
- }
//把表情转成网页能显示型的HTML格式(&#数字;)微信直接显示
- #define MULITTHREEBYTEUTF16TOUNICODE(x,y) (((((x ^ 0xD800) << 2) | ((y ^ 0xDC00) >> 8)) << 8) | ((y ^ 0xDC00) & 0xFF)) + 0x10000
- - (NSString *)emojiUnicodeToHTML{
-
- NSString *hexstr = @"";
-
- if ([self length] >= 2) {
-
- for (int i = 0; i < [self length] / 2 && ([self length] % 2 == 0) ; i++)
-
- {
- // three bytes
- if (([self characterAtIndex:i*2] & 0xFF00) == 0 ) {
- hexstr = [hexstr stringByAppendingFormat:@"Ox%1X 0x%1X",[self characterAtIndex:i*2],[self characterAtIndex:i*2+1]];
- }
- else
- {// four bytes
- hexstr = [hexstr stringByAppendingFormat:@"%1X ",MULITTHREEBYTEUTF16TOUNICODE([self characterAtIndex:i*2],[self characterAtIndex:i*2+1])];
- }
-
- }
- NSLog(@"(unicode) [%@]",hexstr);
- }
- else
-
- {
- NSLog(@"(unicode) U+%1X",[self characterAtIndex:0]);
- }
-
- NSString *html = [NSString stringWithFormat:@"%lu",strtoul([hexstr UTF8String],0,16)];
-
- return [NSString stringWithFormat:@"&#%@;",html];
- }
//解析网页形式的表情格式在UILabel上显示
//把微信端表情转成Unicode编码(如果能支持unicode 字符串 那么直接获取U+1F604 输出即可,如果不支持,那么获取png图片显示即可,下面的convertSimpleUnicodeStr方法获取)
//把微信端的HTML形式的表情转换成iOS端显示用的Unicode格式
匹配HTML格式表情正则.png
- - (NSString *)digitalEmojiToUnicode:(NSString *)string
- {
- //把微信端上传的表情处理成iOS字符串
- NSString *text = [string htmlToString];
- //匹配HTML格式表情正则
- NSString *prefix = @"[^&#]*\\;";
- NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:prefix
- options:NSRegularExpressionCaseInsensitive
- error:nil];
- // 对text字符串进行匹配
- NSArray *matches = [regular matchesInString:text
- options:0
- range:NSMakeRange(0, text.length)];
- // 遍历匹配后的每一条记录
- NSString *result = text;
- for (NSTextCheckingResult *match in matches) {
- NSRange range = [match range];
- NSString *target = [text substringWithRange:range];
- NSLog(@"%@", target);
- //把HTML格式的表情转换为Unicode格式的
- NSString *emojiS = [target emojiHTMLToUnicode];
- //iOS 端直接支持unicode字符 (有一些高级表情会失败,返回nil)
- NSString *convertUnicode = [emojiS convertSimpleUnicodeStr];
- if (convertUnicode.length ==0) {
- convertUnicode = @"❓";(高级表情显示不出来)
- }
- //把表情替换回原来的位置,然后就能直接用UILabel显示表情了
- result = [result stringByReplacingOccurrencesOfString:[@"&#" stringByAppendingString:target] withString:convertUnicode];
-
- }
- return result;
- }
注意:上面代码里面的正则表达式不对,符号被简书改了,看图片。
- - (NSString *)emojiHTMLToUnicode{
-
- NSString *result = [self stringByReplacingOccurrencesOfString:@"&#" withString:@""];
- result = [result stringByReplacingOccurrencesOfString:@";" withString:@""];
- NSString *hexString = [NSString stringWithFormat:@"U+%@",[[NSString alloc] initWithFormat:@"%1X",[result intValue]]];
- return hexString;
- }
//这段函数作用是将 U+1F591 转换为 \U0001F591
//如果为 U+2500 那么就是补全为 \U00002500
//返回的sendStr就是可以直接在UILabel上显示的表情
- - (NSString *)convertSimpleUnicodeStr{
- NSString *strUrl = [self stringByReplacingOccurrencesOfString:@"U+" withString:@""];
- unsigned long unicodeIntValue= strtoul([strUrl UTF8String],0,16);
- // UTF32Char inputChar = unicodeIntValue ;// 变成utf32
- unsigned long inputChar = unicodeIntValue ;// 变成utf32
- // inputChar = NSSwapHostIntToLittle(inputChar); // 转换成Little 如果需要
- inputChar = NSSwapHostLongToLittle(inputChar); // 转换成Little 如果需要
- NSString *sendStr = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
- NSLog(@"%@",sendStr);
- return sendStr;
}
//网页内容转String
- - (NSString *)htmlToString{
-
- NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
- if (data.length > 0) {
- return [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInteger:NSUTF8StringEncoding]} documentAttributes:nil error:nil].string;
- }
- return @"";
- }
注意:这个方法是把网页格式的数据变成字符串
慢慢来,一步一个巴掌印。。。。。
链接:https://www.jianshu.com/p/8436bdd30769
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。