iOS emoji表情轉碼 或者判斷

若是項目中有評論或者信息恢復的地方,每每會用到emoji,有時候如後臺不支持emoji,就會顯示亂碼錯誤,咱們能夠把emoji轉成unicode編碼或者utf8編碼格式傳給服務器。固然若是後臺服務器接收的時候能作好判斷識別最好,咱們這邊後臺是支持的,我僅記錄一下方法,以備不時之需。服務器

先定義一個UITextView 並設置代理 編碼

設定一個宏定義,用來判斷emoji spa

 #define MULITTHREEBYTEUTF16TOUNICODE(x,y) (((((x ^ 0xD800) << 2) | ((y ^ 0xDC00) >> 8)) << 8) | ((y ^ 0xDC00) & 0xFF)) + 0x10000

 

下面寫代理方法實現的內容3d

 1 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 2 {
 3     NSString *hexstr = @"";
 4     
 5     for (int i=0;i< [text length];i++)
 6     {
 7         hexstr = [hexstr stringByAppendingFormat:@"%@",[NSString stringWithFormat:@"0x%1X ",[text characterAtIndex:i]]];
 8     }
 9     NSLog(@"UTF16 [%@]",hexstr);
10     
11     hexstr = @"";
12     
13     long slen = strlen([text UTF8String]);
14     
15     for (int i = 0; i < slen; i++)
16     {
17         //fffffff0 去除前面六個F & 0xFF
18         hexstr = [hexstr stringByAppendingFormat:@"%@",[NSString stringWithFormat:@"0x%X ",[text UTF8String][i] & 0xFF ]];
19     }
20     NSLog(@"UTF8 [%@]",hexstr);
21     
22     hexstr = @"";
23     
24     if ([text length] >= 2) {
25         
26         for (int i = 0; i < [text length] / 2 && ([text length] % 2 == 0) ; i++)
27         {
28             // three bytes
29             if (([text characterAtIndex:i*2] & 0xFF00) == 0 ) {
30                 hexstr = [hexstr stringByAppendingFormat:@"Ox%1X 0x%1X",[text characterAtIndex:i*2],[text characterAtIndex:i*2+1]];
31             }
32             else
33             {// four bytes
34                 hexstr = [hexstr stringByAppendingFormat:@"U+%1X ",MULITTHREEBYTEUTF16TOUNICODE([text characterAtIndex:i*2],[text characterAtIndex:i*2+1])];
35             }
36             
37         }
38         NSLog(@"(unicode) [%@]",hexstr);
39     }
40     else
41     {
42         NSLog(@"(unicode) U+%1X",[text characterAtIndex:0]);
43     }
44     
45     return YES;
46 }

在輸入的時候,會自動把輸入內容轉成相應的格式。代理

 

 

若是在有些地方不須要輸入emoji表情,能夠作相關限制。code

我這邊用到的是,若是用戶輸入emoji表情的時候,會給出提示orm

 1 //是否含有表情
 2 - (BOOL)stringContainsEmoji:(NSString *)string
 3 {
 4     __block BOOL returnValue = NO;
 5     
 6     [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
 7                                options:NSStringEnumerationByComposedCharacterSequences
 8                             usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
 9                                 const unichar hs = [substring characterAtIndex:0];
10                                 if (0xd800 <= hs && hs <= 0xdbff) {
11                                     if (substring.length > 1) {
12                                         const unichar ls = [substring characterAtIndex:1];
13                                         const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
14                                         if (0x1d000 <= uc && uc <= 0x1f77f) {
15                                             returnValue = YES;
16                                         }
17                                     }
18                                 } else if (substring.length > 1) {
19                                     const unichar ls = [substring characterAtIndex:1];
20                                     if (ls == 0x20e3) {
21                                         returnValue = YES;
22                                     }
23                                 } else {
24                                     if (0x2100 <= hs && hs <= 0x27ff) {
25                                         returnValue = YES;
26                                     } else if (0x2B05 <= hs && hs <= 0x2b07) {
27                                         returnValue = YES;
28                                     } else if (0x2934 <= hs && hs <= 0x2935) {
29                                         returnValue = YES;
30                                     } else if (0x3297 <= hs && hs <= 0x3299) {
31                                         returnValue = YES;
32                                     } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
33                                         returnValue = YES;
34                                     }
35                                 }
36                             }];
37     
38     return returnValue;
39 }

經過調用該方法,若是返回的是YES則輸入內容含有emoji,反之。blog

相關文章
相關標籤/搜索