CustomTextSimpleEmoticonParser.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // CustomYYLabel.m
  3. // zhuxun
  4. //
  5. // Created by XuXiaoJun on 16/6/22.
  6. //
  7. //
  8. #import "CustomTextSimpleEmoticonParser.h"
  9. #import "NSString+YYAdd.h"
  10. #import "NSBundle+YYAdd.h"
  11. #import <YYImage/YYImage.h>
  12. @implementation CustomTextSimpleEmoticonParser
  13. + (CustomTextSimpleEmoticonParser *)shareYYCustomTextSimpleEmoticonParser
  14. {
  15. static CustomTextSimpleEmoticonParser *parser = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. // dispatch_async(dispatch_get_main_queue(), ^{
  19. //注册表情规则
  20. NSMutableDictionary *mapper = [NSMutableDictionary new];
  21. NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmotionZhuxue" ofType:@"bundle"]];
  22. NSString *path = [bundle pathForScaledResource:@"expression" ofType:@"plist"];
  23. NSDictionary *expressDict = [[NSDictionary alloc]initWithContentsOfFile:path];
  24. NSArray *allKeyArray = [expressDict allKeys];
  25. NSArray *allValueArray = [expressDict allValues];
  26. //key = code
  27. //value = pic
  28. for (int i = 0; i < allKeyArray.count; i ++) {
  29. NSString *emotionCode = allKeyArray[i];
  30. NSString *emotionPic = allValueArray[i];
  31. mapper[emotionCode] = [CustomTextSimpleEmoticonParser imageWithName:emotionPic];
  32. }
  33. parser = [CustomTextSimpleEmoticonParser new];
  34. parser.emoticonMapper = mapper;
  35. });
  36. return parser;
  37. }
  38. + (UIImage *)imageWithName:(NSString *)name {
  39. NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmotionZhuxue" ofType:@"bundle"]];
  40. NSString *path = [bundle pathForScaledResource:name ofType:@"gif"];
  41. NSData *data = [NSData dataWithContentsOfFile:path];
  42. YYImage *image = [YYImage imageWithData:data scale:2];
  43. image.preloadAllAnimatedImageFrames = YES;
  44. return image;
  45. }
  46. @end