JCHATChatModel.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // JCHATChatModel.m
  3. // test project
  4. //
  5. // Created by guan jingFen on 14-3-10.
  6. // Copyright (c) 2014年 guan jingFen. All rights reserved.
  7. //
  8. #import "JCHATChatModel.h"
  9. #import "JChatConstants.h"
  10. #import "JCHATStringUtils.h"
  11. #define headHeight 46
  12. static NSInteger const voiceBubbleHeight = 50;
  13. @implementation JCHATChatModel
  14. - (instancetype)init {
  15. self = [super init];
  16. if (self) {
  17. _isTime = NO;
  18. }
  19. return self;
  20. }
  21. - (void)setChatModelWith:(JMSGMessage *)message conversationType:(JMSGConversation *)conversation userModel:(MessageListModel *)userModel {
  22. _message = message;
  23. _messageTime = message.timestamp;
  24. //用户 id 和 头像 使用业务服务器的信息
  25. if (_message.isReceived) {
  26. _uidStr = userModel.uidStr;
  27. _uiconStr = userModel.iconStr;
  28. }else {
  29. _uidStr = [Config getOwnID];
  30. _uiconStr = [Config getUserAvatar];
  31. }
  32. switch (message.contentType) {
  33. case kJMSGContentTypeUnknown:
  34. {
  35. if (message.content == nil) {
  36. [self getTextHeight];
  37. }
  38. }
  39. break;
  40. case kJMSGContentTypeText:
  41. {
  42. [self getTextHeight];
  43. }
  44. break;
  45. case kJMSGContentTypeImage:
  46. {
  47. [self setupImageSize];
  48. }
  49. break;
  50. case kJMSGContentTypeFile:{
  51. if (self.message.status == kJMSGMessageStatusReceiveDownloadFailed) {
  52. _contentSize = CGSizeMake(77, 57);
  53. return;
  54. }
  55. self.imageSize = CGSizeMake(215, 67);
  56. _contentSize = CGSizeMake(217, 67);
  57. }
  58. break;
  59. case kJMSGContentTypeLocation:
  60. {
  61. if (self.message.status == kJMSGMessageStatusReceiveDownloadFailed) {
  62. _contentSize = CGSizeMake(77, 57);
  63. return;
  64. }
  65. self.imageSize = CGSizeMake(217+20, 136+35);
  66. _contentSize = CGSizeMake(217+20, 136+35);
  67. }
  68. break;
  69. case kJMSGContentTypeVoice:
  70. {
  71. [self setupVoiceSize:((JMSGVoiceContent *)message.content).duration];
  72. }
  73. break;
  74. case kJMSGContentTypeEventNotification:
  75. {
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. [self getTextHeight];
  82. }
  83. - (void)setErrorMessageChatModelWithError:(NSError *)error{
  84. _isErrorMessage = YES;
  85. _messageError = error;
  86. [self getTextSizeWithString:st_receiveErrorMessageDes];
  87. }
  88. - (float)getTextHeight {
  89. switch (self.message.contentType) {
  90. case kJMSGContentTypeUnknown:
  91. {
  92. [self getTextSizeWithString:st_receiveUnknowMessageDes];
  93. }
  94. break;
  95. case kJMSGContentTypeText:
  96. {
  97. [self getTextSizeWithString:((JMSGTextContent *)self.message.content).text];
  98. } break;
  99. case kJMSGContentTypeImage:
  100. {
  101. //NSLog(@"---");
  102. }
  103. break;
  104. case kJMSGContentTypeVoice:
  105. {
  106. }
  107. break;
  108. case kJMSGContentTypeEventNotification:
  109. {
  110. // [self getTextSizeWithString:[((JMSGEventContent *)self.message.content) showEventNotification]];
  111. [self getNotificationWithString:[((JMSGEventContent *)self.message.content) showEventNotification]];
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. return self.contentHeight;
  118. }
  119. - (NSArray <NSTextCheckingResult *> *)machesWithPattern:(NSString *)pattern andStr:(NSString *)str {
  120. NSError *error = nil;
  121. NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
  122. if (error) {
  123. NSLog(@"正则表达式创建失败");
  124. return nil;
  125. }
  126. return [expression matchesInString:str options:0 range:NSMakeRange(0, str.length)];
  127. }
  128. - (CGSize)getTextSizeWithString:(NSString *)string {
  129. CGSize maxSize = CGSizeMake(200, 2000);
  130. UIFont *font =[UIFont systemFontOfSize:18];
  131. NSMutableParagraphStyle *paragraphStyle= [[NSMutableParagraphStyle alloc] init];
  132. CGSize realSize = [string boundingRectWithSize:maxSize options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
  133. //筛选表情,计算尺寸
  134. NSString *imStr = ((JMSGTextContent *)self.message.content).text;
  135. if (imStr) {
  136. //过渡Label用于计算size
  137. UILabel *imLabel = [[UILabel alloc]init];
  138. imLabel.font = SYS_Font(18);
  139. imLabel.numberOfLines = 0;
  140. imLabel.size = maxSize;
  141. //匹配表情文字
  142. NSString *pattern = emojiPattern;
  143. NSArray *resultArr = [self machesWithPattern:pattern andStr:imStr];
  144. if(resultArr) {
  145. NSUInteger lengthDetail = 0;
  146. NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:imStr];
  147. //遍历所有的result 取出range
  148. for (NSTextCheckingResult *result in resultArr) {
  149. //取出图片名
  150. NSString *imageName = [imStr substringWithRange:NSMakeRange(result.range.location, result.range.length)];
  151. NSLog(@"--------%@",imageName);
  152. if ([lagType isEqual:EN]) {
  153. if ([imageName isEqual:@"[互粉]"] ||
  154. [imageName isEqual:@"[关注]"] ||
  155. [imageName isEqual:@"[粉]"] ||
  156. [imageName isEqual:@"[给力]"]) {
  157. imageName = [imageName stringByAppendingFormat:@"_en"];
  158. }
  159. }
  160. NSTextAttachment *attach = [[NSTextAttachment alloc] init];
  161. UIImage *emojiImage = [UIImage imageNamed:imageName];
  162. NSAttributedString *imageString;
  163. if (emojiImage) {
  164. /*
  165. attach.image = emojiImage;
  166. attach.bounds = CGRectMake(0, -5, 21, 21);
  167. imageString = [NSAttributedString attributedStringWithAttachment:attach];
  168. */
  169. UIImageView *imageV = [[UIImageView alloc] init];
  170. imageV.frame = CGRectMake(0, -2, 15, 15);
  171. imageV.image = emojiImage;
  172. imageString = [NSAttributedString yy_attachmentStringWithContent:imageV contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(15, 15) alignToFont:font alignment:YYTextVerticalAlignmentCenter];
  173. }else{
  174. imageString = [[NSMutableAttributedString alloc]initWithString:imageName];
  175. }
  176. //图片附件的文本长度是1
  177. NSLog(@"emoj===%zd===size-w:%f==size-h:%f",imageString.length,imageString.size.width,imageString.size.height);
  178. NSUInteger length = attstr.length;
  179. NSRange newRange = NSMakeRange(result.range.location - lengthDetail, result.range.length);
  180. [attstr replaceCharactersInRange:newRange withAttributedString:imageString];
  181. lengthDetail += length - attstr.length;
  182. }
  183. imLabel.attributedText = attstr;
  184. [imLabel sizeToFit];
  185. realSize = imLabel.frame.size;
  186. }
  187. }
  188. CGSize imgSize =realSize;
  189. imgSize.height=realSize.height+20;
  190. imgSize.width=realSize.width+2*15;
  191. _contentSize = imgSize;
  192. _contentHeight = imgSize.height;
  193. return imgSize;
  194. }
  195. - (CGSize)getNotificationWithString:(NSString *)string {
  196. CGSize notiSize= [JCHATStringUtils stringSizeWithWidthString:string withWidthLimit:280 withFont:[UIFont systemFontOfSize:14]];
  197. _contentHeight = notiSize.height;
  198. _contentSize = notiSize;
  199. return notiSize;
  200. }
  201. - (void)setupImageSize {
  202. if (self.message.status == kJMSGMessageStatusReceiveDownloadFailed) {
  203. _contentSize = CGSizeMake(77, 57);
  204. return;
  205. }
  206. JMSGImageContent *imageContent = (JMSGImageContent *)self.message.content;
  207. float imgHeight;
  208. float imgWidth;
  209. if (imageContent.imageSize.height >= imageContent.imageSize.width) {
  210. imgHeight = 135;
  211. imgWidth = (imageContent.imageSize.width/imageContent.imageSize.height) *imgHeight;
  212. } else {
  213. imgWidth = 135;
  214. imgHeight = (imageContent.imageSize.height/imageContent.imageSize.width) *imgWidth;
  215. }
  216. if ((imgWidth > imgHeight?imgHeight/imgWidth:imgWidth/imgHeight)<0.47) {
  217. self.imageSize = imgWidth > imgHeight?CGSizeMake(135, 55):CGSizeMake(55, 135);//
  218. _contentSize = imgWidth > imgHeight?CGSizeMake(135, 55):CGSizeMake(55, 135);
  219. return;
  220. }
  221. self.imageSize = CGSizeMake(imgWidth, imgHeight);
  222. _contentSize = CGSizeMake(imgWidth, imgHeight);
  223. // [((JMSGImageContent *)self.message.content) thumbImageData:^(NSData *data, NSString *objectId, NSError *error) {
  224. // if (error == nil) {
  225. // UIImage *img = [UIImage imageWithData:data];
  226. // float imgHeight;
  227. // float imgWidth;
  228. //
  229. // if (img.size.height >= img.size.width) {
  230. // imgHeight = 135;
  231. // imgWidth = (img.size.width/img.size.height) *imgHeight;
  232. // } else {
  233. // imgWidth = 135;
  234. // imgHeight = (img.size.height/img.size.width) *imgWidth;
  235. // }
  236. //
  237. // if ((imgWidth > imgHeight?imgHeight/imgWidth:imgWidth/imgHeight)<0.47) {
  238. // self.imageSize = imgWidth > imgHeight?CGSizeMake(135, 55):CGSizeMake(55, 135);//
  239. // _contentSize = imgWidth > imgHeight?CGSizeMake(135, 55):CGSizeMake(55, 135);
  240. // return;
  241. // }
  242. // self.imageSize = CGSizeMake(imgWidth, imgHeight);
  243. // _contentSize = CGSizeMake(imgWidth, imgHeight);
  244. // } else {
  245. // NSLog(@"get thumbImageData fail with error %@",error);
  246. // }
  247. // }];
  248. }
  249. - (float)getLengthWithDuration:(NSInteger)duration {
  250. NSInteger voiceBubbleWidth = 0;
  251. if (duration <= 2) {
  252. voiceBubbleWidth = 60;
  253. } else if (duration >2 && duration <=20) {
  254. voiceBubbleWidth = 60 + 2.5 * duration;
  255. } else if (duration > 20 && duration < 30){
  256. voiceBubbleWidth = 110 + 2 * (duration - 20);
  257. } else if (duration >30 && duration < 60) {
  258. voiceBubbleWidth = 130 + 1 * (duration - 30);
  259. } else {
  260. voiceBubbleWidth = 160;
  261. }
  262. _contentSize = CGSizeMake(voiceBubbleWidth, voiceBubbleHeight);
  263. return voiceBubbleWidth;
  264. }
  265. - (void)setupVoiceSize:(NSNumber *)timeduration {
  266. NSInteger voiceBubbleWidth = 0;
  267. NSInteger duration = [timeduration integerValue];
  268. if (duration <= 2) {
  269. voiceBubbleWidth = 60;
  270. } else if (duration >2 && duration <=20) {
  271. voiceBubbleWidth = 60 + 2.5 * duration;
  272. } else if (duration > 20 && duration < 30){
  273. voiceBubbleWidth = 110 + 2 * (duration - 20);
  274. } else if (duration >30 && duration < 60) {
  275. voiceBubbleWidth = 130 + 1 * (duration - 30);
  276. } else {
  277. voiceBubbleWidth = 160;
  278. }
  279. _contentSize = CGSizeMake(voiceBubbleWidth, voiceBubbleHeight);
  280. }
  281. @end