NSString+LZBTranscoding.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NSString+LZBTranscoding.m
  3. // LZBKeyBoardView
  4. //
  5. // demo地址:https://github.com/lzbgithubcode/LZBKeyBoardView.git
  6. // Created by zibin on 16/12/6.
  7. // Copyright © 2016年 apple. All rights reserved.
  8. //
  9. #import "NSString+LZBTranscoding.h"
  10. //#define EMOJI_CODE_TO_SYMBOL(x) (((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000))
  11. #define EMOJI_CODE_TO_SYMBOL(c) ((((0x808080F0 | (c & 0x3F000) >> 4) | (c & 0xFC0) << 10) | (c & 0x1C0000) << 18) | (c & 0x3F) << 24)
  12. @implementation NSString (LZBTranscoding)
  13. + (NSString *)emojiWithByIntCode:(int)intCode
  14. {
  15. int sym = EMOJI_CODE_TO_SYMBOL(intCode);
  16. NSString *codeString = [[NSString alloc]initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
  17. if (codeString == nil) {
  18. codeString = [NSString stringWithFormat:@"%C", (unichar)intCode];
  19. }
  20. return codeString;
  21. }
  22. + (NSString *)emojiWithByStringCode:(NSString *)stringCode
  23. {
  24. char *charCode = (char *)stringCode.UTF8String;
  25. long intCode = strtol(charCode, NULL, 16);
  26. return [self emojiWithByIntCode:(int)intCode];
  27. }
  28. /**
  29. * 转为emoji字符
  30. */
  31. - (NSString *)emoji
  32. {
  33. return [NSString emojiWithByStringCode:self];
  34. }
  35. - (BOOL)isContainsEmoji
  36. {
  37. __block BOOL returnValue = NO;
  38. if(self.length == 0) return returnValue;
  39. [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
  40. options:NSStringEnumerationByComposedCharacterSequences
  41. usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
  42. const unichar hs = [substring characterAtIndex:0];
  43. if (0xd800 <= hs && hs <= 0xdbff)
  44. {
  45. if (substring.length > 1)
  46. {
  47. const unichar ls = [substring characterAtIndex:1];
  48. const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  49. if (0x1d000 <= uc && uc <= 0x1f77f)
  50. {
  51. returnValue = YES;
  52. }
  53. }
  54. }
  55. else if (substring.length > 1)
  56. {
  57. const unichar ls = [substring characterAtIndex:1];
  58. if (ls == 0x20e3)
  59. {
  60. returnValue = YES;
  61. }
  62. }
  63. else
  64. {
  65. if (0x2100 <= hs && hs <= 0x27ff)
  66. {
  67. returnValue = YES;
  68. }
  69. else if (0x2B05 <= hs && hs <= 0x2b07)
  70. {
  71. returnValue = YES;
  72. }
  73. else if (0x2934 <= hs && hs <= 0x2935)
  74. {
  75. returnValue = YES;
  76. }
  77. else if (0x3297 <= hs && hs <= 0x3299)
  78. {
  79. returnValue = YES;
  80. }
  81. else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
  82. {
  83. returnValue = YES;
  84. }
  85. }
  86. }];
  87. return returnValue;
  88. }
  89. @end