YOUPAINIMInputEmoticonManager.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // NTESEmoticonManager.h
  3. // NIM
  4. //
  5. // Created by amao on 7/2/14.
  6. // Copyright (c) 2014 Netease. All rights reserved.
  7. //
  8. #import "YOUPAINIMInputEmoticonManager.h"
  9. #import "NIMInputEmoticonDefine.h"
  10. #import "NSString+NIMKit.h"
  11. #import "NIMKit.h"
  12. @implementation NIMInputEmoticon
  13. @end
  14. @implementation NIMInputEmoticonCatalog
  15. @end
  16. @implementation NIMInputEmoticonLayout
  17. - (id)initEmojiLayout:(CGFloat)width
  18. {
  19. self = [super init];
  20. if (self)
  21. {
  22. _rows = NIMKit_EmojRows;
  23. _columes = ((width - NIMKit_EmojiLeftMargin - NIMKit_EmojiRightMargin) / NIMKit_EmojImageWidth);
  24. _itemCountInPage = _rows * _columes -1;
  25. _cellWidth = (width - NIMKit_EmojiLeftMargin - NIMKit_EmojiRightMargin) / _columes;
  26. _cellHeight = NIMKit_EmojCellHeight;
  27. _imageWidth = NIMKit_EmojImageWidth;
  28. _imageHeight = NIMKit_EmojImageHeight;
  29. _emoji = YES;
  30. }
  31. return self;
  32. }
  33. - (id)initCharletLayout:(CGFloat)width{
  34. self = [super init];
  35. if (self)
  36. {
  37. _rows = NIMKit_PicRows;
  38. _columes = ((width - NIMKit_EmojiLeftMargin - NIMKit_EmojiRightMargin) / NIMKit_PicImageWidth);
  39. _itemCountInPage = _rows * _columes;
  40. _cellWidth = (width - NIMKit_EmojiLeftMargin - NIMKit_EmojiRightMargin) / _columes;
  41. _cellHeight = NIMKit_PicCellHeight;
  42. _imageWidth = NIMKit_PicImageWidth;
  43. _imageHeight = NIMKit_PicImageHeight;
  44. _emoji = NO;
  45. }
  46. return self;
  47. }
  48. @end
  49. @interface YOUPAINIMInputEmoticonManager ()
  50. @property (nonatomic,strong) NSArray *catalogs;
  51. @end
  52. @implementation YOUPAINIMInputEmoticonManager
  53. + (instancetype)sharedManager
  54. {
  55. static YOUPAINIMInputEmoticonManager *instance = nil;
  56. static dispatch_once_t onceToken;
  57. dispatch_once(&onceToken, ^{
  58. instance = [[YOUPAINIMInputEmoticonManager alloc]init];
  59. });
  60. return instance;
  61. }
  62. - (id)init
  63. {
  64. if (self = [super init])
  65. {
  66. [self parsePlist];
  67. }
  68. return self;
  69. }
  70. - (NIMInputEmoticonCatalog *)youpaifemoticonCatalog:(NSString *)catalogID
  71. {
  72. for (NIMInputEmoticonCatalog *catalog in _catalogs)
  73. {
  74. if ([catalog.catalogID isEqualToString:catalogID])
  75. {
  76. return catalog;
  77. }
  78. }
  79. return nil;
  80. }
  81. - (NIMInputEmoticon *)emoticonByTag:(NSString *)tag
  82. {
  83. NIMInputEmoticon *emoticon = nil;
  84. if ([tag length])
  85. {
  86. for (NIMInputEmoticonCatalog *catalog in _catalogs)
  87. {
  88. emoticon = [catalog.tag2Emoticons objectForKey:tag];
  89. if (emoticon)
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. return emoticon;
  96. }
  97. - (NIMInputEmoticon *)youpaifemoticonByID:(NSString *)emoticonID
  98. {
  99. NIMInputEmoticon *emoticon = nil;
  100. if ([emoticonID length])
  101. {
  102. for (NIMInputEmoticonCatalog *catalog in _catalogs)
  103. {
  104. emoticon = [catalog.id2Emoticons objectForKey:emoticonID];
  105. if (emoticon)
  106. {
  107. break;
  108. }
  109. }
  110. }
  111. return emoticon;
  112. }
  113. - (NIMInputEmoticon *)youpaifemoticonByCatalogID:(NSString *)catalogID
  114. emoticonID:(NSString *)emoticonID
  115. {
  116. NIMInputEmoticon *emoticon = nil;
  117. if ([emoticonID length] && [catalogID length])
  118. {
  119. for (NIMInputEmoticonCatalog *catalog in _catalogs)
  120. {
  121. if ([catalog.catalogID isEqualToString:catalogID])
  122. {
  123. emoticon = [catalog.id2Emoticons objectForKey:emoticonID];
  124. break;
  125. }
  126. }
  127. }
  128. return emoticon;
  129. }
  130. - (void)parsePlist
  131. {
  132. NSMutableArray *catalogs = [NSMutableArray array];
  133. NSURL *url = [[NSBundle mainBundle] URLForResource:[[NIMKit sharedKit] emoticonBundleName]
  134. withExtension:nil];
  135. NSBundle *bundle = [NSBundle bundleWithURL:url];
  136. NSString *filepath = [bundle pathForResource:@"emoji" ofType:@"plist" inDirectory:NIMKit_EmojiPath];
  137. if (filepath) {
  138. NSArray *array = [NSArray arrayWithContentsOfFile:filepath];
  139. for (NSDictionary *dict in array)
  140. {
  141. NSDictionary *info = dict[@"info"];
  142. NSArray *emoticons = dict[@"data"];
  143. NIMInputEmoticonCatalog *catalog = [self catalogByInfo:info
  144. emoticons:emoticons];
  145. [catalogs addObject:catalog];
  146. }
  147. }
  148. _catalogs = catalogs;
  149. }
  150. - (NIMInputEmoticonCatalog *)catalogByInfo:(NSDictionary *)info
  151. emoticons:(NSArray *)emoticonsArray
  152. {
  153. NIMInputEmoticonCatalog *catalog = [[NIMInputEmoticonCatalog alloc]init];
  154. catalog.catalogID = info[@"id"];
  155. catalog.title = info[@"title"];
  156. NSString *iconNamePrefix = NIMKit_EmojiPath;
  157. NSString *icon = info[@"normal"];
  158. catalog.icon = [iconNamePrefix stringByAppendingPathComponent:icon];
  159. NSString *iconPressed = info[@"pressed"];
  160. catalog.iconPressed = [iconNamePrefix stringByAppendingPathComponent:iconPressed];
  161. NSMutableDictionary *tag2Emoticons = [NSMutableDictionary dictionary];
  162. NSMutableDictionary *id2Emoticons = [NSMutableDictionary dictionary];
  163. NSMutableArray *emoticons = [NSMutableArray array];
  164. for (NSDictionary *emoticonDict in emoticonsArray) {
  165. NIMInputEmoticon *emoticon = [[NIMInputEmoticon alloc] init];
  166. emoticon.emoticonID = emoticonDict[@"id"];
  167. emoticon.tag = emoticonDict[@"tag"];
  168. NSString *fileName = emoticonDict[@"file"];
  169. NSString *imageNamePrefix = NIMKit_EmojiPath;
  170. emoticon.filename = [imageNamePrefix stringByAppendingPathComponent:fileName];
  171. if (emoticon.emoticonID) {
  172. [emoticons addObject:emoticon];
  173. id2Emoticons[emoticon.emoticonID] = emoticon;
  174. }
  175. if (emoticon.tag) {
  176. tag2Emoticons[emoticon.tag] = emoticon;
  177. }
  178. }
  179. catalog.emoticons = emoticons;
  180. catalog.id2Emoticons = id2Emoticons;
  181. catalog.tag2Emoticons = tag2Emoticons;
  182. return catalog;
  183. }
  184. @end