QMChatRoomRobotCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // QMChatRoomRobotCell.m
  3. // IMSDK-OC
  4. //
  5. // Created by haochongfeng on 2018/1/23.
  6. // Copyright © 2018年 HCF. All rights reserved.
  7. //
  8. #import "QMChatRoomRobotCell.h"
  9. #import "QMChatRoomRobotReplyView.h"
  10. #import <QMLineSDK/QMLineSDK.h>
  11. #import "QMTextModel.h"
  12. #import "QMTapGestureRecognizer.h"
  13. #import "QMChatRoomShowImageController.h"
  14. @interface QMChatRoomRobotCell() <MLEmojiLabelDelegate>
  15. @end
  16. @implementation QMChatRoomRobotCell
  17. {
  18. NSString *_messageId;
  19. QMChatRoomRobotReplyView *_replyView;
  20. // 内容高度
  21. CGFloat height;
  22. // 内容宽度
  23. CGFloat width;
  24. // 链接集合
  25. NSMutableArray *srcArrs;
  26. }
  27. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  28. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  29. if (self) {
  30. srcArrs = [NSMutableArray array];
  31. }
  32. return self;
  33. }
  34. - (void)setData:(CustomMessage *)message avater:(NSString *)avater {
  35. self.message = message;
  36. _messageId = message._id;
  37. [super setData:message avater:avater];
  38. [srcArrs removeAllObjects];
  39. if ([message.fromType isEqualToString:@"1"]) {
  40. height = 10;
  41. width = 0;
  42. for(UIView *view in self.chatBackgroudImage.subviews){
  43. if(view){
  44. [view removeFromSuperview];
  45. }
  46. }
  47. //html标签替换
  48. message.message = [message.message stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];
  49. message.message = [message.message stringByReplacingOccurrencesOfString:@"</br>" withString:@"\n"];
  50. message.message = [message.message stringByReplacingOccurrencesOfString:@"</p>" withString:@"\n"];
  51. NSMutableArray * srcArr = [self showHtml:message.message];
  52. //获取分段类型 文本 图片
  53. for (QMTextModel *model in srcArr) {
  54. if ([model.type isEqualToString:@"text"]) {
  55. [self createLabel:model.content];
  56. }else {
  57. [self createImage:model.content];
  58. }
  59. }
  60. //机器人评价
  61. _replyView = [[QMChatRoomRobotReplyView alloc] init];
  62. _replyView.backgroundColor = [UIColor clearColor];
  63. [_replyView.helpBtn addTarget:self action:@selector(helpBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  64. [_replyView.noHelpBtn addTarget:self action:@selector(noHelpBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  65. [self.chatBackgroudImage addSubview:_replyView];
  66. if ([message.isRobot isEqualToString:@"1"] && ![message.questionId isEqualToString:@""]) {
  67. [_replyView setHidden:NO];
  68. if (message.isUseful) {
  69. _replyView.status = message.isUseful;
  70. if ([message.isUseful isEqualToString:@"none"]) {
  71. _replyView.frame = CGRectMake(10, height + 5, [UIScreen mainScreen].bounds.size.width - 150, 25);
  72. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, [UIScreen mainScreen].bounds.size.width-130, height+15 + 30);
  73. }else {
  74. _replyView.frame = CGRectMake(10, height + 5, [UIScreen mainScreen].bounds.size.width - 150, 55);
  75. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, [UIScreen mainScreen].bounds.size.width-130, height+15 + 60);
  76. }
  77. }else {
  78. _replyView.status = @"none";
  79. _replyView.frame = CGRectMake(10, height + 5, [UIScreen mainScreen].bounds.size.width - 150, 25);
  80. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, [UIScreen mainScreen].bounds.size.width-130, height+15 + 30);
  81. }
  82. }else {
  83. [_replyView setHidden:YES];
  84. _replyView.frame = CGRectZero;
  85. self.chatBackgroudImage.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, CGRectGetMaxY(self.timeLabel.frame)+10, width+30, height+15);
  86. }
  87. }
  88. }
  89. // 创建文本
  90. - (void)createLabel: (NSString *)text {
  91. NSMutableArray *array = [self getAHtml:text];
  92. NSRegularExpression *regularExpretion = [[NSRegularExpression alloc] initWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:nil];
  93. NSString *tempString = [regularExpretion stringByReplacingMatchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length) withTemplate:@""];
  94. MLEmojiLabel *tLabel = [MLEmojiLabel new];
  95. tLabel.numberOfLines = 0;
  96. tLabel.font = [UIFont systemFontOfSize:14.0f];
  97. tLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  98. tLabel.delegate = self;
  99. tLabel.disableEmoji = NO;
  100. tLabel.disableThreeCommon = NO;
  101. tLabel.isNeedAtAndPoundSign = YES;
  102. tLabel.customEmojiRegex = @"\\:[^\\:]+\\:";
  103. tLabel.customEmojiPlistName = @"expressionImage.plist";
  104. tLabel.customEmojiBundleName = @"QMEmoticon.bundle";
  105. [self.chatBackgroudImage addSubview:tLabel];
  106. tLabel.checkResults = array;
  107. tLabel.checkColor = [UIColor colorWithRed:32/255.0f green:188/255.0f blue:158/255.0f alpha:1];
  108. tLabel.text = tempString;
  109. // labelFrame
  110. CGSize size = [tLabel preferredSizeWithMaxWidth: [UIScreen mainScreen].bounds.size.width - 160];
  111. tLabel.frame = CGRectMake(15, height, size.width, size.height);
  112. // 宽高适配
  113. height += size.height;
  114. width = width > size.width ? width : size.width;
  115. }
  116. // 创建图片 图片大小可调整
  117. - (void)createImage: (NSString *)imageUrl {
  118. NSArray *temArray = nil;
  119. if ([imageUrl rangeOfString:@"src=\""].location != NSNotFound) {
  120. temArray = [imageUrl componentsSeparatedByString:@"src=\""];
  121. }else if ([imageUrl rangeOfString:@"src="].location != NSNotFound) {
  122. temArray = [imageUrl componentsSeparatedByString:@"src="];
  123. }
  124. if (temArray.count >= 2) {
  125. NSString *src = temArray[1];
  126. NSUInteger loc = [src rangeOfString:@"\""].location;
  127. if (loc != NSNotFound) {
  128. // 图片地址
  129. src = [src substringToIndex:loc];
  130. src = [src stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  131. UIImageView *imageView = [[UIImageView alloc] init];
  132. imageView.frame = CGRectMake(15, height, 140, 100);
  133. imageView.userInteractionEnabled = YES;
  134. imageView.contentMode = UIViewContentModeScaleAspectFill;
  135. imageView.clipsToBounds = YES;
  136. [self.chatBackgroudImage addSubview:imageView];
  137. [imageView sd_setImageWithURL:[NSURL URLWithString:src] placeholderImage:[UIImage imageNamed:@"icon"]];
  138. QMTapGestureRecognizer * tapPressGesture = [[QMTapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressGesture:)];
  139. tapPressGesture.picName = src;
  140. tapPressGesture.picType = @"1";
  141. tapPressGesture.image = imageView.image;
  142. [imageView addGestureRecognizer:tapPressGesture];
  143. height += 100;
  144. }
  145. }
  146. }
  147. #pragma mark 文本处理
  148. - (NSMutableArray *)showHtml: (NSString *)htmlString {
  149. // 拆分文本和图片
  150. __block NSString *tempString = htmlString;
  151. __block NSMutableArray *srcArr = [NSMutableArray array];
  152. NSRegularExpression *regularExpretion = [[NSRegularExpression alloc] initWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:nil];
  153. [regularExpretion enumerateMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, [htmlString length]) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
  154. if (result.range.length != 0) {
  155. // 字符串
  156. NSString *actionString = [NSString stringWithFormat:@"%@",[htmlString substringWithRange:result.range]];
  157. // 新的range
  158. NSRange range = [tempString rangeOfString:actionString];
  159. NSArray *components = nil;
  160. if ([actionString rangeOfString:@"<img src=\""].location != NSNotFound) {
  161. components = [actionString componentsSeparatedByString:@"src=\""];
  162. }else if ([actionString rangeOfString:@"<img src="].location != NSNotFound) {
  163. components = [actionString componentsSeparatedByString:@"src="];
  164. }
  165. if (components.count >= 2) {
  166. // 文本内容
  167. QMTextModel *model1 = [[QMTextModel alloc] init];
  168. model1.type = @"text";
  169. model1.content = [tempString substringToIndex:range.location];
  170. [srcArr addObject:model1];
  171. // 图片内容
  172. QMTextModel *model2 = [[QMTextModel alloc] init];
  173. model2.type = @"image";
  174. model2.content = [tempString substringWithRange:range];;
  175. [srcArr addObject:model2];
  176. tempString = [tempString substringFromIndex:range.location+range.length];
  177. }
  178. }
  179. }];
  180. QMTextModel *model3 = [[QMTextModel alloc] init];
  181. model3.type = @"text";
  182. model3.content = tempString;
  183. [srcArr addObject:model3];
  184. return srcArr;
  185. }
  186. - (NSMutableArray *)getAHtml: (NSString *)htmlString {
  187. // 文本匹配A标签
  188. __block NSString *tempString = htmlString;
  189. __block NSMutableArray *srcArr = [NSMutableArray array];
  190. __block int length = 0;
  191. NSRegularExpression *regularExpretion = [[NSRegularExpression alloc] initWithPattern:@"<a href=(?:.*?)>(.*?)</a>" options:NSRegularExpressionCaseInsensitive error:nil];
  192. [regularExpretion enumerateMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, [htmlString length]) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
  193. if (result.range.length != 0) {
  194. NSRegularExpression *regularExpretion1 = [[NSRegularExpression alloc] initWithPattern:@"<[^>]*>" options:NSRegularExpressionCaseInsensitive error:nil];
  195. QMTextModel *model = [[QMTextModel alloc] init];
  196. // 获取高亮字符串
  197. NSString *actionString = [NSString stringWithFormat:@"%@",[htmlString substringWithRange:result.range]];
  198. // 获取链接 actionString -> https
  199. NSArray *components = [actionString componentsSeparatedByString:@"href=\""];
  200. if (components.count > 1) {
  201. NSString *src = components[1];
  202. NSUInteger loc = [src rangeOfString:@"\""].location;
  203. if (loc != NSNotFound) {
  204. // 地址
  205. src = [src substringToIndex:loc];
  206. src = [src stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  207. model.content = src;
  208. }
  209. }
  210. actionString = [regularExpretion1 stringByReplacingMatchesInString:actionString options:NSMatchingReportProgress range:NSMakeRange(0, actionString.length) withTemplate:@""];
  211. // 获取高亮range 防止重复
  212. actionString = [NSString stringWithFormat:@">%@<", actionString];
  213. model.type = actionString;
  214. NSRange range = [tempString rangeOfString:actionString];
  215. // 高亮之前的字符串
  216. if (tempString.length > range.location+1) {
  217. NSString *preString = [tempString substringToIndex:range.location+1];
  218. preString = [regularExpretion1 stringByReplacingMatchesInString:preString options:NSMatchingReportProgress range:NSMakeRange(0, preString.length) withTemplate:@""];
  219. actionString = [NSString stringWithFormat:@"at->%@",actionString];
  220. NSTextCheckingResult *aResult = [NSTextCheckingResult correctionCheckingResultWithRange:NSMakeRange(preString.length+length, range.length-2) replacementString:actionString];
  221. // 截取掉a标签前的字符串(防止a标签名称重复)
  222. tempString = [tempString substringFromIndex:range.location+1];
  223. // 字符串截取部分的长度
  224. length += preString.length;
  225. [srcArr addObject:aResult];
  226. [srcArrs addObject:model];
  227. }
  228. }
  229. }];
  230. return srcArr;
  231. }
  232. #pragma mark MLEmojiLabelDelegate
  233. - (void)mlEmojiLabel:(MLEmojiLabel *)emojiLabel didSelectLink:(NSString *)link withType:(MLEmojiLabelLinkType)type {
  234. switch (type) {
  235. case 1:
  236. self.tapNetAddress(link);
  237. break;
  238. case 3:
  239. for (QMTextModel *model in srcArrs) {
  240. if ([model.type isEqualToString:link]) {
  241. self.tapNetAddress(model.content);
  242. break;
  243. }
  244. }
  245. break;
  246. default:{
  247. NSString *newLink = [link stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  248. NSArray *array = [newLink componentsSeparatedByString:@":"];
  249. if (array.count > 1) {
  250. self.tapSendMessage(array[1]);
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. #pragma mark 点击图片GestureRecognizer
  257. - (void)imagePressGesture:(QMTapGestureRecognizer *)gestureRecognizer {
  258. QMChatRoomShowImageController * showPicVC = [[QMChatRoomShowImageController alloc] init];
  259. showPicVC.picName = gestureRecognizer.picName;
  260. showPicVC.picType = gestureRecognizer.picType;
  261. showPicVC.image = gestureRecognizer.image;
  262. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:showPicVC animated:true completion:nil];
  263. }
  264. #pragma mark 机器人答案反馈点击事件
  265. - (void)helpBtnAction: (UIButton *)sender {
  266. self.didBtnAction(YES);
  267. }
  268. - (void)noHelpBtnAction: (UIButton *)sender {
  269. self.didBtnAction(NO);
  270. }
  271. - (void)awakeFromNib {
  272. [super awakeFromNib];
  273. // Initialization code
  274. }
  275. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  276. [super setSelected:selected animated:animated];
  277. // Configure the view for the selected state
  278. }
  279. @end