EaseEmotionEscape.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseEmotionEscape.h"
  13. #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
  14. #define kEmotionTopMargin -3.0f
  15. @implementation EMTextAttachment
  16. //I want my emoticon has the same size with line's height
  17. - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0)
  18. {
  19. return CGRectMake( 0, kEmotionTopMargin, lineFrag.size.height, lineFrag.size.height);
  20. }
  21. @end
  22. @interface EaseEmotionEscape ()
  23. {
  24. NSString *_urlPattern;
  25. NSDictionary *_dict;
  26. }
  27. @end
  28. @implementation EaseEmotionEscape
  29. static EaseEmotionEscape *_sharedInstance = nil;
  30. + (EaseEmotionEscape *)sharedInstance
  31. {
  32. if (_sharedInstance == nil)
  33. {
  34. @synchronized(self) {
  35. _sharedInstance = [[EaseEmotionEscape alloc] init];
  36. }
  37. }
  38. return _sharedInstance;
  39. }
  40. + (NSMutableAttributedString *) attributtedStringFromText:(NSString *) aInputText
  41. {
  42. return nil;
  43. }
  44. + (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText
  45. {
  46. return nil;
  47. }
  48. + (NSAttributedString *) attStringFromTextForInputView:(NSString *) aInputText
  49. {
  50. return nil;
  51. }
  52. - (NSMutableAttributedString *) attributtedStringFromText:(NSString *) aInputText
  53. {
  54. if (_urlPattern == nil || _urlPattern.length == 0) {
  55. NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:aInputText attributes:nil ];
  56. return string;
  57. }
  58. NSString *urlPattern = _urlPattern;
  59. NSError *error = nil;
  60. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlPattern options:NSRegularExpressionCaseInsensitive error:&error ];
  61. NSArray* matches = [regex matchesInString:aInputText options:NSMatchingReportCompletion range:NSMakeRange(0, [aInputText length])];
  62. NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:aInputText attributes:nil ];
  63. for (NSTextCheckingResult *match in [matches reverseObjectEnumerator]) {
  64. NSRange matchRange = [match range];
  65. NSString *subStr = [aInputText substringWithRange:matchRange];
  66. EMTextAttachment * textAttachment = [[EMTextAttachment alloc ] initWithData:nil ofType:nil];
  67. textAttachment.imageName = subStr;
  68. UIImage * emojiImage;
  69. NSString *emojiName = @"";
  70. if (_dict) {
  71. emojiName = [_dict objectForKey:subStr];
  72. }
  73. if (emojiName == nil || emojiName.length == 0) {
  74. emojiName = subStr;
  75. }
  76. emojiImage = [UIImage imageNamed:emojiName];
  77. NSAttributedString * textAttachmentString;
  78. if (emojiImage) {
  79. textAttachment.image = emojiImage ;
  80. textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
  81. }else{
  82. NSString *str = [self getEmojiTextByKey:subStr];
  83. if (str != nil) {
  84. str = [NSString stringWithFormat:@"[%@]", str];
  85. textAttachmentString = [[NSAttributedString alloc] initWithString:str];
  86. }else {
  87. textAttachmentString = [[NSAttributedString alloc] initWithString:@"[表情]"];
  88. }
  89. }
  90. if (textAttachment != nil) {
  91. [string deleteCharactersInRange:matchRange];
  92. [string insertAttributedString:textAttachmentString atIndex:matchRange.location];
  93. }
  94. }
  95. return string;
  96. }
  97. - (NSAttributedString *) attStringFromTextForChatting:(NSString *) aInputText textFont:(UIFont*)font
  98. {
  99. NSMutableAttributedString * string = [self attributtedStringFromText:aInputText];
  100. // NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  101. // paragraphStyle.lineSpacing = 0.0;
  102. // [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
  103. if (font) {
  104. [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
  105. }
  106. return string;
  107. }
  108. - (NSAttributedString *) attStringFromTextForInputView:(NSString *) aInputText textFont:(UIFont*)font
  109. {
  110. NSMutableAttributedString * string = [self attributtedStringFromText:aInputText];
  111. // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  112. // paragraphStyle.lineSpacing = 0.0;
  113. // [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
  114. if (font) {
  115. [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
  116. }
  117. return string;
  118. }
  119. - (NSString*) getEmojiTextByKey:(NSString*) aKey
  120. {
  121. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  122. NSString *plistPaht = [paths objectAtIndex:0];
  123. NSString *fileName = [plistPaht stringByAppendingPathComponent:@"EmotionTextMapList.plist"];
  124. NSMutableDictionary *emojiKeyValue = [[NSMutableDictionary alloc] initWithContentsOfFile: fileName];
  125. return [emojiKeyValue objectForKey:aKey];
  126. // NSLog(@"write data is :%@",writeData);
  127. }
  128. - (NSString*) getEmojiImageNameByKey:(NSString*) aKey
  129. {
  130. return nil;
  131. }
  132. - (void) setEaseEmotionEscapePattern:(NSString *)pattern
  133. {
  134. _urlPattern = pattern;
  135. }
  136. - (void) setEaseEmotionEscapeDictionary:(NSDictionary*)dict
  137. {
  138. _dict = dict;
  139. }
  140. @end