JXReplyCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // JXReplyCell.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2019/3/30.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXReplyCell.h"
  9. #define lineInset 5 // 加大 可增加与line的间隙
  10. @interface JXReplyCell ()
  11. @property (nonatomic, strong)UIView *line;
  12. @end
  13. @implementation JXReplyCell
  14. - (void)dealloc {
  15. [self.readDelTimer invalidate];
  16. self.readDelTimer = nil;
  17. }
  18. -(void)creatUI{
  19. _replyConent = [[JXEmoji alloc] init];
  20. _replyConent.lineBreakMode = NSLineBreakByWordWrapping;
  21. _replyConent.numberOfLines = 0;
  22. _replyConent.backgroundColor = [UIColor clearColor];
  23. _replyConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  24. _replyConent.userInteractionEnabled = YES;
  25. [self.bubbleBg addSubview:_replyConent];
  26. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didReplyContent)];
  27. [_replyConent addGestureRecognizer:tap];
  28. _line = [[UIView alloc] init];
  29. _line.backgroundColor = THE_LINE_COLOR;
  30. [self.bubbleBg addSubview:_line];
  31. _messageConent=[[JXEmoji alloc] init];
  32. _messageConent.lineBreakMode = NSLineBreakByWordWrapping;
  33. _messageConent.numberOfLines = 0;
  34. _messageConent.backgroundColor = [UIColor clearColor];
  35. _messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  36. [self.bubbleBg addSubview:_messageConent];
  37. _timeIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
  38. _timeIndexLabel.layer.cornerRadius = _timeIndexLabel.frame.size.width / 2;
  39. _timeIndexLabel.layer.masksToBounds = YES;
  40. _timeIndexLabel.textColor = [UIColor whiteColor];
  41. _timeIndexLabel.backgroundColor = HEXCOLOR(0x02d8c9);
  42. _timeIndexLabel.textAlignment = NSTextAlignmentCenter;
  43. _timeIndexLabel.text = @"0";
  44. _timeIndexLabel.font = [UIFont systemFontOfSize:12.0];
  45. _timeIndexLabel.hidden = YES;
  46. [self.contentView addSubview:_timeIndexLabel];
  47. }
  48. -(void)setCellData{
  49. [super setCellData];
  50. JXMessageObject *msgObj = [[JXMessageObject alloc] init];
  51. _messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  52. _messageConent.frame = CGRectMake(0, 0, 200, 20);
  53. if (self.msg.objectId.length > 0) {
  54. if ([self.msg.type intValue] != kWCMessageTypeReply) {
  55. _messageConent.atUserIdS = self.msg.objectId;
  56. }else {
  57. SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
  58. NSDictionary *dict = [jsonParser objectWithString:self.msg.objectId];
  59. [msgObj fromDictionary:dict];
  60. }
  61. }
  62. _replyConent.textColor = [UIColor grayColor];
  63. _replyConent.text = [NSString stringWithFormat:@"%@:%@",msgObj.fromUserName,[msgObj getTypeName]];
  64. if ([self.msg.isReadDel boolValue] && [self.msg.fileName length] <= 0 && !self.msg.isMySend) {
  65. _messageConent.userInteractionEnabled = NO;
  66. _messageConent.text = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
  67. _messageConent.textColor = HEXCOLOR(0x0079FF);
  68. _timeIndexLabel.hidden = YES;
  69. }else {
  70. _messageConent.userInteractionEnabled = YES;
  71. _messageConent.textColor = HEXCOLOR(0x333333);
  72. _messageConent.text = self.msg.content;
  73. _timeIndexLabel.hidden = YES;
  74. if (!self.msg.isMySend && [self.msg.fileName isKindOfClass:[NSString class]] && [self.msg.fileName length] > 0 && [self.msg.fileName intValue] >= 0) {
  75. self.timeIndexLabel.hidden = NO;
  76. NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
  77. NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
  78. NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
  79. CGSize size = [messageText boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(g_constant.chatFont)} context:nil].size;
  80. NSInteger count = size.height / _messageConent.font.lineHeight;
  81. NSLog(@"countcount === %ld-----%f-----%@",count,[[NSDate date] timeIntervalSince1970],self.msg.fileName);
  82. // NSLog(@"countcount === %ld,,,,%f,,,,%@",count,[[NSDate date] timeIntervalSince1970], self.msg.fileName);
  83. count = count * 10 - ([[NSDate date] timeIntervalSince1970] - [self.msg.fileName longLongValue]);
  84. self.timerIndex = count;
  85. NSLog(@"countcount1 === %ld",count);
  86. if (count > 0) {
  87. self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",count];
  88. if (!self.readDelTimer) {
  89. self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  90. }
  91. }else {
  92. self.msg.fileName = @"0";
  93. //阅后即焚通知
  94. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  95. [self deleteMsg:self.msg];
  96. }
  97. }
  98. }
  99. [self creatBubbleBg];
  100. }
  101. -(void)creatBubbleBg{
  102. CGSize textSize = _messageConent.frame.size;
  103. CGSize replySize = _replyConent.frame.size;
  104. int n = textSize.width > replySize.width ? textSize.width : replySize.width;
  105. //聊天长度反正就是算错了,强行改
  106. if(n){
  107. // n -= 10;
  108. }
  109. if(self.msg.isMySend){
  110. self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)-INSETS*2-n-CHAT_WIDTH_ICON+2, INSETS, n+INSETS*2, textSize.height+replySize.height+INSETS*2+lineInset*2+LINE_WH);
  111. [_replyConent setFrame:CGRectMake(INSETS*0.4 + 3, INSETS, n + 5, replySize.height)];
  112. [_line setFrame:CGRectMake(INSETS*0.4 + 3, CGRectGetMaxY(_replyConent.frame)+lineInset, n + 5, LINE_WH)];
  113. [_messageConent setFrame:CGRectMake(INSETS*0.4 + 3, CGRectGetMaxY(_line.frame)+lineInset, n + 5, textSize.height)];
  114. _timeIndexLabel.frame = CGRectMake(self.bubbleBg.frame.origin.x - 30, self.bubbleBg.frame.origin.y, 20, 20);
  115. // _messageConent.textAlignment = NSTextAlignmentRight;
  116. }else
  117. {
  118. self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup), n+INSETS*2, textSize.height+replySize.height+INSETS*2+lineInset*2+.5);
  119. [_replyConent setFrame:CGRectMake(INSETS + 3, INSETS, n + 5, replySize.height)];
  120. [_line setFrame:CGRectMake(INSETS + 3, CGRectGetMaxY(_replyConent.frame)+lineInset, n + 5, .5)];
  121. [_messageConent setFrame:CGRectMake(INSETS + 3, CGRectGetMaxY(_line.frame)+lineInset, n + 5, textSize.height)];
  122. _timeIndexLabel.frame = CGRectMake(CGRectGetMaxX(self.bubbleBg.frame) + 10, self.bubbleBg.frame.origin.y, 20, 20);
  123. // _messageConent.textAlignment = NSTextAlignmentLeft;
  124. }
  125. if (self.msg.isShowTime) {
  126. CGRect frame = self.bubbleBg.frame;
  127. frame.origin.y = self.bubbleBg.frame.origin.y + 40;
  128. self.bubbleBg.frame = frame;
  129. _timeIndexLabel.frame = CGRectMake(_timeIndexLabel.frame.origin.x, self.bubbleBg.frame.origin.y, 20, 20);
  130. }
  131. }
  132. - (void)setBackgroundImage {
  133. [super setBackgroundImage];
  134. if (!self.msg.isMySend && [self.msg.fileName isKindOfClass:[NSString class]] && [self.msg.fileName length] > 0 && [self.msg.fileName intValue] >= 0 && [self.msg.type intValue] == kWCMessageTypeText) {
  135. self.isDidMsgCell = YES;
  136. }
  137. if ([self.msg.isReadDel boolValue] && !self.msg.isMySend && self.isDidMsgCell) {
  138. [self drawReadDelView:YES];
  139. self.isDidMsgCell = NO;
  140. }
  141. }
  142. //复制信息到剪贴板
  143. - (void)myCopy{
  144. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  145. [pasteboard setString:self.msg.content];
  146. }
  147. + (float)getChatCellHeight:(JXMessageObject *)msg {
  148. JXMessageObject *msgObj = [[JXMessageObject alloc] init];
  149. if ([msg.type intValue] == kWCMessageTypeReply) {
  150. SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
  151. NSDictionary *dict = [jsonParser objectWithString:msg.objectId];
  152. [msgObj fromDictionary:dict];
  153. }
  154. NSString *fromStr = [NSString stringWithFormat:@"%@:%@",msgObj.fromUserName,[msgObj getTypeName]];
  155. NSString *toStr;
  156. if ([msg.isReadDel boolValue] && [msg.fileName intValue] <= 0 && !msg.isMySend) {
  157. NSString *str = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
  158. toStr = [NSString stringWithFormat:@"%@\n%@",fromStr,str];
  159. }else {
  160. toStr = [NSString stringWithFormat:@"%@\n%@",fromStr,msg.content];
  161. }
  162. if ([msg.chatMsgHeight floatValue] > 1) {
  163. return [msg.chatMsgHeight floatValue];
  164. }
  165. float n;
  166. JXEmoji *messageConent=[[JXEmoji alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
  167. messageConent.backgroundColor = [UIColor clearColor];
  168. // messageConent.userInteractionEnabled = NO;
  169. messageConent.numberOfLines = 0;
  170. messageConent.lineBreakMode = NSLineBreakByWordWrapping;//UILineBreakModeWordWrap;
  171. messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  172. messageConent.offset = -12;
  173. messageConent.frame = CGRectMake(0, 0, 200, 20);
  174. messageConent.text = toStr;
  175. if (msg.isGroup && !msg.isMySend) {
  176. n = messageConent.frame.size.height+10*3 + 20;
  177. if (msg.isShowTime) {
  178. n=messageConent.frame.size.height+10*3 + 40 + 20;
  179. }
  180. n += GROUP_CHAT_INSET;
  181. }else {
  182. n= messageConent.frame.size.height+10*3 + 10;
  183. if (msg.isShowTime) {
  184. n=messageConent.frame.size.height+10*3 + 40 + 10;
  185. }
  186. }
  187. // NSLog(@"heightForRowAtIndexPath_%d,%d:=%@",indexPath.row,n,_messageConent.text);
  188. if(n<55)
  189. n = 55;
  190. if (msg.isShowTime) {
  191. if(n<95)
  192. n = 95;
  193. }
  194. //lineInset*2+.5 为中间和line 的间隙
  195. n += lineInset*2+.5;
  196. msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
  197. if (!msg.isNotUpdateHeight) {
  198. [msg updateChatMsgHeight];
  199. }
  200. return n;
  201. }
  202. - (void)didReplyContent {
  203. [g_notify postNotificationName:kCellReplyNotifaction object:[NSNumber numberWithInt:self.indexNum]];
  204. }
  205. -(void)didTouch:(UIButton*)button{
  206. if ([self.msg.isReadDel boolValue] && [self.msg.fileName intValue] <= 0 && !self.msg.isMySend) {
  207. [self.msg sendAlreadyReadMsg];
  208. self.msg.fileName = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
  209. [self.msg updateFileName];
  210. self.timeIndexLabel.hidden = NO;
  211. _messageConent.text = self.msg.content;
  212. // NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
  213. // NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
  214. // NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
  215. // CGSize size = [messageText boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(g_constant.chatFont)} context:nil].size;
  216. // NSInteger count = size.height / _messageConent.font.lineHeight;
  217. // self.msg.fileName = [NSString stringWithFormat:@"%ld", count * 10];
  218. self.isDidMsgCell = YES;
  219. self.msg.chatMsgHeight = [NSString stringWithFormat:@"0"];
  220. [self.msg updateChatMsgHeight];
  221. [g_notify postNotificationName:kCellMessageReadDelNotifaction object:[NSNumber numberWithInt:self.indexNum]];
  222. // self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  223. }
  224. }
  225. - (void)timerAction:(NSTimer *)timer {
  226. if (self.timerIndex <= 0) {
  227. [self.readDelTimer invalidate];
  228. self.readDelTimer = nil;
  229. self.msg.fileName = @"0";
  230. //阅后即焚通知
  231. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  232. [self deleteMsg:self.msg];
  233. return;
  234. }
  235. self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",-- self.timerIndex];
  236. // self.msg.fileName = self.timeIndexLabel.text;
  237. // [self.msg updateFileName];
  238. }
  239. - (void)deleteMsg:(JXMessageObject *)msg{
  240. if ([self.msg.isReadDel boolValue]) {
  241. if ([self.msg.fileName intValue] > 0) {
  242. return;
  243. }
  244. //渐变隐藏
  245. [UIView animateWithDuration:2.f animations:^{
  246. self.bubbleBg.alpha = 0;
  247. self.timeIndexLabel.alpha = 0;
  248. self.readImage.alpha = 0;
  249. self.burnImage.alpha = 0;
  250. } completion:^(BOOL finished) {
  251. //动画结束后删除UI
  252. [self.delegate performSelectorOnMainThread:self.readDele withObject:msg waitUntilDone:NO];
  253. self.bubbleBg.alpha = 1;
  254. self.timeIndexLabel.alpha = 1;
  255. self.readImage.alpha = 1;
  256. self.burnImage.alpha = 1;
  257. }];
  258. }
  259. }
  260. @end