IOS emoji expression transcoding or judging

Source: Internet
Author: User

If there are comments or information recovery in the project place, often use emoji, sometimes such as the background does not support emoji, will show garbled error, we can turn emoji into Unicode encoding or UTF8 encoding format to the server. Of course, if the background server receives the time can be well judged to identify the best, our side backstage is supported, I only record a method, in case of a rainy day.

First define a Uitextview and set up the proxy

Set a macro definition that is used to determine emoji

#define Mulitthreebyteutf16tounicode (x, Y) (((((((0xD800) << 2) | ((y ^ 0xdc00) >> 8)) << 8) | ((y ^ 0xdc00) & 0xFF)) + 0x10000

What the following write Agent method implements

1-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) Text2 {3NSString *hexstr =@"";4     5      for(intI=0;i< [Text length];i++)6     {7HEXSTR = [Hexstr Stringbyappendingformat:@"%@", [NSString stringWithFormat:@"0x%1x", [text Characteratindex:i]];8     }9NSLog (@"UTF16 [%@]", hexstr);Ten      OneHexstr =@""; A      -     LongSlen =strlen ([text utf8string]); -      the      for(inti =0; i < Slen; i++) -     { -         //fffffff0 removal of front six F & 0xFF -HEXSTR = [Hexstr Stringbyappendingformat:@"%@", [NSString stringWithFormat:@"0x%X", [Text Utf8string][i] &0xFF ]]; +     } -NSLog (@"UTF8 [%@]", hexstr); +      AHexstr =@""; at      -     if([Text Length] >=2) { -          -          for(inti =0; i < [text length]/2&& ([text length]%2==0) ; i++) -         { -             //three bytes in             if([Text characteratindex:i*2] &0xff00) ==0 ) { -HEXSTR = [Hexstr Stringbyappendingformat:@"ox%1x 0x%1x", [Text characteratindex:i*2],[text characteratindex:i*2+1]]; to             } +             Else -{//Four bytes theHEXSTR = [Hexstr Stringbyappendingformat:@"u+%1x", Mulitthreebyteutf16tounicode ([Text characteratindex:i*2],[text characteratindex:i*2+1])]; *             } $             Panax Notoginseng         } -NSLog (@"(Unicode) [%@]", hexstr); the     } +     Else A     { theNSLog (@"(Unicode) u+%1x", [Text Characteratindex:0]); +     } -      $     returnYES; $}

The input is automatically converted to the appropriate format when it is entered.

If you do not need to enter emoji expression in some places, you can do the relevant restrictions.

What I use here is, if the user input emoji expression, will give a hint

1 //does it contain emoticons ?2-(BOOL) Stringcontainsemoji: (NSString *)string3 {4__block BOOL returnvalue =NO;5     6[stringEnumeratesubstringsinrange:nsmakerange (0, [stringlength])7 options:nsstringenumerationbycomposedcharactersequences8usingblock:^ (NSString *substring, Nsrange substringrange, Nsrange enclosingrange, BOOL *stop) {9                                 ConstUnichar HS = [substring Characteratindex:0];Ten                                 if(0xd800<= HS && HS <=0XDBFF) { One                                     if(Substring.length >1) { A                                         ConstUnichar ls = [substring characteratindex:1]; -                                         Const intUC = (HS-0xd800) *0x400) + (LS-0xdc00) +0x10000; -                                         if(0x1d000<= UC && UC <=0x1f77f) { theReturnValue =YES; -                                         } -                                     } -}Else if(Substring.length >1) { +                                     ConstUnichar ls = [substring characteratindex:1]; -                                     if(ls = =0x20e3) { +ReturnValue =YES; A                                     } at}Else { -                                     if(0x2100<= HS && HS <=0x27ff) { -ReturnValue =YES; -}Else if(0x2b05<= HS && HS <=0x2b07) { -ReturnValue =YES; -}Else if(0x2934<= HS && HS <=0x2935) { inReturnValue =YES; -}Else if(0x3297<= HS && HS <=0x3299) { toReturnValue =YES; +}Else if(HS = =0xa9|| HS = =0xae|| HS = =0x303d|| HS = =0x3030|| HS = =0x2b55|| HS = =0x2b1c|| HS = =0x2b1b|| HS = =0x2b50) { -ReturnValue =YES; the                                     } *                                 } $                             }];Panax Notoginseng      -     returnreturnvalue; the}

By calling this method, if the return is yes then the input contains emoji, and vice versa.

IOS emoji expression transcoding or judging

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.