EMEmojiHelper.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // EMEmojiHelper.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/31.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMEmojiHelper.h"
  9. #define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24);
  10. static EMEmojiHelper *helper = nil;
  11. @implementation EMEmojiHelper
  12. - (instancetype)init
  13. {
  14. self = [super init];
  15. if (self) {
  16. _convertEmojiDic = @{@"[):]":@"😊", @"[:D]":@"😃", @"[;)]":@"😉", @"[:-o]":@"😮", @"[:p]":@"😋", @"[(H)]":@"😎", @"[:@]":@"😡", @"[:s]":@"😖", @"[:$]":@"😳", @"[:(]":@"😞", @"[:'(]":@"😭", @"[:|]":@"😐", @"[(a)]":@"😇", @"[8o|]":@"😬", @"[8-|]":@"😆", @"[+o(]":@"😱", @"[<o)]":@"🎅", @"[|-)]":@"😴", @"[*-)]":@"😕", @"[:-#]":@"😷", @"[:-*]":@"😯", @"[^o)]":@"😏", @"[8-)]":@"😑", @"[(|)]":@"💖", @"[(u)]":@"💔", @"[(S)]":@"🌙", @"[(*)]":@"🌟", @"[(#)]":@"🌞", @"[(R)]":@"🌈", @"[(})]":@"😚", @"[({)]":@"😍", @"[(k)]":@"💋", @"[(F)]":@"🌹", @"[(W)]":@"🍂", @"[(D)]":@"👍"};
  17. }
  18. return self;
  19. }
  20. + (instancetype)sharedHelper
  21. {
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. helper = [[EMEmojiHelper alloc] init];
  25. });
  26. return helper;
  27. }
  28. + (NSString *)emojiWithCode:(int)aCode
  29. {
  30. int sym = EMOJI_CODE_TO_SYMBOL(aCode);
  31. return [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
  32. }
  33. + (NSArray<NSString *> *)getAllEmojis
  34. {
  35. NSArray *emojis = @[[EMEmojiHelper emojiWithCode:0x1F60a],
  36. [EMEmojiHelper emojiWithCode:0x1F603],
  37. [EMEmojiHelper emojiWithCode:0x1F609],
  38. [EMEmojiHelper emojiWithCode:0x1F62e],
  39. [EMEmojiHelper emojiWithCode:0x1F60b],
  40. [EMEmojiHelper emojiWithCode:0x1F60e],
  41. [EMEmojiHelper emojiWithCode:0x1F621],
  42. [EMEmojiHelper emojiWithCode:0x1F616],
  43. [EMEmojiHelper emojiWithCode:0x1F633],
  44. [EMEmojiHelper emojiWithCode:0x1F61e],
  45. [EMEmojiHelper emojiWithCode:0x1F62d],
  46. [EMEmojiHelper emojiWithCode:0x1F610],
  47. [EMEmojiHelper emojiWithCode:0x1F607],
  48. [EMEmojiHelper emojiWithCode:0x1F62c],
  49. [EMEmojiHelper emojiWithCode:0x1F606],
  50. [EMEmojiHelper emojiWithCode:0x1F631],
  51. [EMEmojiHelper emojiWithCode:0x1F385],
  52. [EMEmojiHelper emojiWithCode:0x1F634],
  53. [EMEmojiHelper emojiWithCode:0x1F615],
  54. [EMEmojiHelper emojiWithCode:0x1F637],
  55. [EMEmojiHelper emojiWithCode:0x1F62f],
  56. [EMEmojiHelper emojiWithCode:0x1F60f],
  57. [EMEmojiHelper emojiWithCode:0x1F611],
  58. [EMEmojiHelper emojiWithCode:0x1F496],
  59. [EMEmojiHelper emojiWithCode:0x1F494],
  60. [EMEmojiHelper emojiWithCode:0x1F319],
  61. [EMEmojiHelper emojiWithCode:0x1f31f],
  62. [EMEmojiHelper emojiWithCode:0x1f31e],
  63. [EMEmojiHelper emojiWithCode:0x1F308],
  64. [EMEmojiHelper emojiWithCode:0x1F60d],
  65. [EMEmojiHelper emojiWithCode:0x1F61a],
  66. [EMEmojiHelper emojiWithCode:0x1F48b],
  67. [EMEmojiHelper emojiWithCode:0x1F339],
  68. [EMEmojiHelper emojiWithCode:0x1F342],
  69. [EMEmojiHelper emojiWithCode:0x1F44d]];
  70. return emojis;
  71. }
  72. + (BOOL)isStringContainsEmoji:(NSString *)aString
  73. {
  74. __block BOOL ret = NO;
  75. [aString enumerateSubstringsInRange:NSMakeRange(0, [aString length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
  76. const unichar hs = [substring characterAtIndex:0];
  77. if (0xd800 <= hs && hs <= 0xdbff) {
  78. if (substring.length > 1) {
  79. const unichar ls = [substring characterAtIndex:1];
  80. const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  81. if (0x1d000 <= uc && uc <= 0x1f77f) {
  82. ret = YES;
  83. }
  84. }
  85. } else if (substring.length > 1) {
  86. const unichar ls = [substring characterAtIndex:1];
  87. if (ls == 0x20e3) {
  88. ret = YES;
  89. }
  90. } else {
  91. if (0x2100 <= hs && hs <= 0x27ff) {
  92. ret = YES;
  93. } else if (0x2B05 <= hs && hs <= 0x2b07) {
  94. ret = YES;
  95. } else if (0x2934 <= hs && hs <= 0x2935) {
  96. ret = YES;
  97. } else if (0x3297 <= hs && hs <= 0x3299) {
  98. ret = YES;
  99. } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
  100. ret = YES;
  101. }
  102. }
  103. }];
  104. return ret;
  105. }
  106. + (NSString *)convertEmoji:(NSString *)aString
  107. {
  108. NSDictionary *emojisDic = [EMEmojiHelper sharedHelper].convertEmojiDic;
  109. NSRange range;
  110. range.location = 0;
  111. NSMutableString *retStr = [NSMutableString stringWithString:aString];
  112. for (NSString *key in emojisDic) {
  113. range.length = retStr.length;
  114. NSString *value = emojisDic[key];
  115. [retStr replaceOccurrencesOfString:key withString:value options:NSLiteralSearch range:range];
  116. }
  117. return retStr;
  118. }
  119. @end