JXNewShakeCell.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // JXMessageCell.m
  3. // shiku_im
  4. //
  5. // Created by Apple on 16/10/10.
  6. // Copyright © 2016年 Reese. All rights reserved.
  7. //
  8. #import "JXNewShakeCell.h"
  9. #import "emojiViewController.h"
  10. #import "EmojiTextAttachment.h"
  11. //#define TEXT_MAX_HEIGHT 500.0f
  12. @implementation JXNewShakeCell
  13. - (void)awakeFromNib {
  14. [super awakeFromNib];
  15. // self.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, 55);
  16. }
  17. - (void)dealloc {
  18. [self.readDelTimer invalidate];
  19. self.readDelTimer = nil;
  20. }
  21. -(void)creatUI{
  22. _messageConent=[[JXEmoji alloc] init];
  23. // _messageConent.userInteractionEnabled = YES;
  24. _messageConent.lineBreakMode = NSLineBreakByWordWrapping;
  25. _messageConent.numberOfLines = 0;
  26. _messageConent.backgroundColor = [UIColor clearColor];
  27. _messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  28. // _messageConent.userInteractionEnabled = NO;
  29. [self.bubbleBg addSubview:_messageConent];
  30. //添加震动小图标
  31. _shakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"震动"]];
  32. [self.bubbleBg addSubview:_shakeImageView];
  33. _timeIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
  34. _timeIndexLabel.layer.cornerRadius = _timeIndexLabel.frame.size.width / 2;
  35. _timeIndexLabel.layer.masksToBounds = YES;
  36. _timeIndexLabel.textColor = [UIColor whiteColor];
  37. _timeIndexLabel.backgroundColor = HEXCOLOR(0x02d8c9);
  38. _timeIndexLabel.textAlignment = NSTextAlignmentCenter;
  39. _timeIndexLabel.text = @"0";
  40. _timeIndexLabel.font = [UIFont systemFontOfSize:12.0];
  41. _timeIndexLabel.hidden = YES;
  42. [self.contentView addSubview:_timeIndexLabel];
  43. }
  44. -(void)setCellData{
  45. [super setCellData];
  46. _messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  47. _messageConent.frame = CGRectMake(0, 0, 200, 20);
  48. if (self.msg.objectId.length > 0) {
  49. _messageConent.atUserIdS = self.msg.objectId;
  50. }
  51. if ([self.msg.isReadDel boolValue] && ([self.msg.fileName length] <= 0 || [self.msg.fileName intValue] <= 0) && !self.msg.isMySend) {
  52. _messageConent.userInteractionEnabled = NO;
  53. _messageConent.text = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
  54. _messageConent.textColor = HEXCOLOR(0x0079FF);
  55. _timeIndexLabel.hidden = YES;
  56. }else {
  57. _messageConent.userInteractionEnabled = YES;
  58. _messageConent.textColor = HEXCOLOR(0x333333);
  59. _messageConent.text = self.msg.content;
  60. _timeIndexLabel.hidden = YES;
  61. if (!self.msg.isMySend && [self.msg.fileName isKindOfClass:[NSString class]] && [self.msg.fileName length] > 0 && [self.msg.fileName intValue] > 0) {
  62. self.timeIndexLabel.hidden = NO;
  63. NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
  64. NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
  65. NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
  66. NSMutableAttributedString * textC = [self changeEmjoyText:messageText];
  67. [textC addAttribute:NSFontAttributeName value:SYSFONT(g_constant.chatFont) range:NSMakeRange(0, textC.length)];
  68. CGSize size = [textC boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
  69. NSInteger count = size.height / _messageConent.font.lineHeight;
  70. NSLog(@"countcount === %ld-----%f-----%@",count,[[NSDate date] timeIntervalSince1970],self.msg.fileName);
  71. // NSLog(@"countcount === %ld,,,,%f,,,,%@",count,[[NSDate date] timeIntervalSince1970], self.msg.fileName);
  72. count = count * 10 - ([[NSDate date] timeIntervalSince1970] - [self.msg.fileName longLongValue]);
  73. self.timerIndex = count;
  74. NSLog(@"countcount1 === %ld",count);
  75. if (count > 0) {
  76. self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",count];
  77. if (!self.readDelTimer) {
  78. self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  79. }
  80. }else {
  81. self.msg.fileName = @"0";
  82. //阅后即焚通知
  83. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  84. [self deleteMsg:self.msg];
  85. }
  86. }
  87. }
  88. [self creatBubbleBg];
  89. }
  90. -(void)creatBubbleBg{
  91. CGSize textSize = _messageConent.frame.size;
  92. int n = textSize.width;
  93. //聊天长度反正就是算错了,强行改
  94. if(n){
  95. // n -= 10;
  96. }
  97. int inset = 3;
  98. if(self.msg.isMySend){
  99. self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)-INSETS*2-inset*2-n-CHAT_WIDTH_ICON - 20, INSETS, n+INSETS*2+inset*2 +20+(INSETS*0.4 + 3+inset), textSize.height+INSETS*2);
  100. [_shakeImageView setFrame:CGRectMake(INSETS*0.4 + 3+inset, INSETS, 20, 20)];
  101. [_messageConent setFrame:CGRectMake(CGRectGetMaxX(_shakeImageView.frame)+INSETS*0.4 + 3+inset, INSETS, n + 5, textSize.height)];
  102. // [_messageConent setFrame:CGRectMake(INSETS*0.4 + 3+inset, INSETS, n + 5, textSize.height)];
  103. _timeIndexLabel.frame = CGRectMake(self.bubbleBg.frame.origin.x - 30, self.bubbleBg.frame.origin.y, 20, 20);
  104. // _messageConent.textAlignment = NSTextAlignmentRight;
  105. }else
  106. {
  107. self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup), n+INSETS*2+inset*2+20 + 2*(INSETS*0.4 + 3+inset), textSize.height+INSETS*2);
  108. [_messageConent setFrame:CGRectMake(INSETS + 3+inset, INSETS, n + 5, textSize.height)];
  109. [_shakeImageView setFrame:CGRectMake(INSETS*0.4 + 3+inset + CGRectGetMaxX(_messageConent.frame), INSETS, 20, 20)];
  110. _timeIndexLabel.frame = CGRectMake(CGRectGetMaxX(self.bubbleBg.frame) + 10, self.bubbleBg.frame.origin.y, 20, 20);
  111. // _messageConent.textAlignment = NSTextAlignmentLeft;
  112. }
  113. if (self.msg.isShowTime) {
  114. CGRect frame = self.bubbleBg.frame;
  115. frame.origin.y = self.bubbleBg.frame.origin.y + 40;
  116. self.bubbleBg.frame = frame;
  117. _timeIndexLabel.frame = CGRectMake(_timeIndexLabel.frame.origin.x, self.bubbleBg.frame.origin.y, 20, 20);
  118. }
  119. }
  120. - (void)setBackgroundImage {
  121. [super setBackgroundImage];
  122. 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) {
  123. self.isDidMsgCell = YES;
  124. }
  125. if ([self.msg.isReadDel boolValue] && !self.msg.isMySend && self.isDidMsgCell) {
  126. [self drawReadDelView:YES];
  127. self.isDidMsgCell = NO;
  128. }
  129. }
  130. //复制信息到剪贴板
  131. - (void)myCopy{
  132. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  133. [pasteboard setString:self.msg.content];
  134. }
  135. + (float)getChatCellHeight:(JXMessageObject *)msg {
  136. if ([msg.chatMsgHeight floatValue] > 1) {
  137. return [msg.chatMsgHeight floatValue];
  138. }
  139. float n;
  140. JXEmoji *messageConent=[[JXEmoji alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
  141. messageConent.backgroundColor = [UIColor clearColor];
  142. // messageConent.userInteractionEnabled = NO;
  143. messageConent.numberOfLines = 0;
  144. messageConent.lineBreakMode = NSLineBreakByWordWrapping;//UILineBreakModeWordWrap;
  145. messageConent.font = [UIFont systemFontOfSize:g_constant.chatFont];
  146. messageConent.offset = -12;
  147. messageConent.frame = CGRectMake(0, 0, 200, 20);
  148. if ([msg.isReadDel boolValue] && [msg.fileName intValue] <= 0 && !msg.isMySend) {
  149. messageConent.text = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
  150. }else {
  151. messageConent.text = msg.content;
  152. }
  153. if (msg.isGroup && !msg.isMySend) {
  154. n = messageConent.frame.size.height+10*3 + 20;
  155. if (msg.isShowTime) {
  156. n=messageConent.frame.size.height+10*3 + 40 + 20;
  157. }
  158. n += GROUP_CHAT_INSET;
  159. }else {
  160. n= messageConent.frame.size.height+10*3 + 10;
  161. if (msg.isShowTime) {
  162. n=messageConent.frame.size.height+10*3 + 40 + 10;
  163. }
  164. }
  165. // NSLog(@"heightForRowAtIndexPath_%d,%d:=%@",indexPath.row,n,_messageConent.text);
  166. if(n<55)
  167. n = 55;
  168. if (msg.isShowTime) {
  169. if(n<95)
  170. n = 95;
  171. }
  172. msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
  173. if (!msg.isNotUpdateHeight) {
  174. [msg updateChatMsgHeight];
  175. }
  176. return n;
  177. }
  178. -(void)didTouch:(UIButton*)button{
  179. if ([self.msg.isReadDel boolValue] && [self.msg.fileName intValue] <= 0 && !self.msg.isMySend) {
  180. [self.msg sendAlreadyReadMsg];
  181. self.msg.fileName = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
  182. [self.msg updateFileName];
  183. self.timeIndexLabel.hidden = NO;
  184. _messageConent.text = self.msg.content;
  185. // NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
  186. // NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
  187. // NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
  188. // CGSize size = [messageText boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(g_constant.chatFont)} context:nil].size;
  189. // NSInteger count = size.height / _messageConent.font.lineHeight;
  190. // self.msg.fileName = [NSString stringWithFormat:@"%ld", count * 10];
  191. self.isDidMsgCell = YES;
  192. self.msg.chatMsgHeight = [NSString stringWithFormat:@"0"];
  193. [self.msg updateChatMsgHeight];
  194. [g_notify postNotificationName:kCellMessageReadDelNotifaction object:[NSNumber numberWithInt:self.indexNum]];
  195. // self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  196. }
  197. }
  198. - (void)timerAction:(NSTimer *)timer {
  199. if (self.timerIndex <= 0) {
  200. [self.readDelTimer invalidate];
  201. self.readDelTimer = nil;
  202. self.msg.fileName = @"0";
  203. //阅后即焚通知
  204. [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
  205. [self deleteMsg:self.msg];
  206. return;
  207. }
  208. self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",-- self.timerIndex];
  209. // self.msg.fileName = self.timeIndexLabel.text;
  210. // [self.msg updateFileName];
  211. }
  212. - (void)deleteMsg:(JXMessageObject *)msg{
  213. if ([self.msg.isReadDel boolValue]) {
  214. if ([self.msg.fileName intValue] > 0) {
  215. return;
  216. }
  217. //渐变隐藏
  218. [UIView animateWithDuration:2.f animations:^{
  219. self.bubbleBg.alpha = 0;
  220. self.timeIndexLabel.alpha = 0;
  221. self.readImage.alpha = 0;
  222. self.burnImage.alpha = 0;
  223. } completion:^(BOOL finished) {
  224. //动画结束后删除UI
  225. [self.delegate performSelectorOnMainThread:self.readDele withObject:msg waitUntilDone:NO];
  226. self.bubbleBg.alpha = 1;
  227. self.timeIndexLabel.alpha = 1;
  228. self.readImage.alpha = 1;
  229. self.burnImage.alpha = 1;
  230. }];
  231. }
  232. }
  233. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  234. [super setSelected:selected animated:animated];
  235. // Configure the view for the selected state
  236. }
  237. #pragma mark - 有表情的txt 转换成 含图片的str
  238. - (NSMutableAttributedString *)changeEmjoyText:(NSString *)text {
  239. NSMutableAttributedString *conStr = [[NSMutableAttributedString alloc] init];
  240. NSMutableArray *arr = [NSMutableArray array];
  241. [self getImageRange:text array:arr];
  242. if (arr.count > 1) {
  243. for (NSInteger i = 0; i < arr.count; i ++) {
  244. NSString *str = arr[i];
  245. NSInteger n;
  246. if ([str hasPrefix:@"["]&&[str hasSuffix:@"]"] && [g_faceVC.shortNameArrayE containsObject:str]) {
  247. n = [g_faceVC.shortNameArrayE indexOfObject:str];
  248. NSDictionary *dic = [g_constant.emojiArray objectAtIndex:n];
  249. EmojiTextAttachment *attachment = [[EmojiTextAttachment alloc] init];
  250. attachment.emojiTag = str;
  251. attachment.image = [UIImage imageNamed:dic[@"filename"]];
  252. attachment.bounds = CGRectMake(0, -4, 23, 23);
  253. [conStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
  254. }else {
  255. [conStr appendAttributedString:[[NSAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:self.messageConent.font}]];
  256. }
  257. }
  258. }else {
  259. return [[NSMutableAttributedString alloc] initWithString:text];
  260. }
  261. return conStr;
  262. }
  263. //将表情和文字分开,装进array
  264. -(void)getImageRange:(NSString*)message array: (NSMutableArray*)array {
  265. NSRange range=[message rangeOfString: @"["];
  266. NSRange range1=[message rangeOfString: @"]"];
  267. NSRange atRange = [message rangeOfString:@"@"];
  268. //判断当前字符串是否还有表情的标志。
  269. if (((range.length>0 && range1.length>0) || atRange.length>0) && range1.location > range.location) {
  270. if (range.length>0 && range1.length>0) {
  271. // self.contentEmoji = YES;
  272. // if (range.location > 0) {
  273. // [array addObject:[message substringToIndex:range.location]];
  274. // [array addObject:[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)]];
  275. // NSString *str=[message substringFromIndex:range1.location+1];
  276. // [self getImageRange:str array:array];
  277. // }else {
  278. // NSString *nextstr=[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
  279. // //排除文字是“”的
  280. // if (![nextstr isEqualToString:@""]) {
  281. // [array addObject:nextstr];
  282. // NSString *str=[message substringFromIndex:range1.location+1];
  283. // [self getImageRange:str array:array];
  284. // }else {
  285. // return;
  286. // }
  287. // }
  288. if (range.location > 0) {
  289. NSString *str = [message substringToIndex:range.location];
  290. NSString *str1 = [message substringFromIndex:range.location];
  291. [array addObject:str];
  292. [self getImageRange:str1 array:array];
  293. }else {
  294. NSString *emojiString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location - 1)];
  295. BOOL isEmoji = NO;
  296. NSString *str;
  297. NSString *str1;
  298. for (NSMutableDictionary *dic in g_constant.emojiArray) {
  299. NSString *emoji = [dic objectForKey:@"english"];
  300. if ([emoji isEqualToString:emojiString]) {
  301. isEmoji = YES;
  302. break;
  303. }
  304. }
  305. if (isEmoji) {
  306. str = [message substringWithRange:NSMakeRange(range.location, range1.location + 1)];
  307. str1 = [message substringFromIndex:range1.location + 1];
  308. [array addObject:str];
  309. }else{
  310. NSString *posString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location)];
  311. NSRange posRange = [posString rangeOfString:@"["];
  312. if (posRange.location != NSNotFound) {
  313. str = [message substringToIndex:posRange.location + 1];
  314. str1 = [message substringFromIndex:posRange.location + 1];
  315. [array addObject:str];
  316. }else{
  317. str = [message substringToIndex:range1.location + 1];
  318. str1 = [message substringFromIndex:range1.location + 1];
  319. [array addObject:str];
  320. }
  321. }
  322. [self getImageRange:str1 array:array];
  323. }
  324. } else if (atRange.length>0) {
  325. if (atRange.location > 0) {
  326. [array addObject:[message substringToIndex:atRange.location]];
  327. [array addObject:[message substringWithRange:NSMakeRange(atRange.location, 1)]];
  328. NSString *str=[message substringFromIndex:atRange.location+1];
  329. [self getImageRange:str array:array];
  330. }else{
  331. [array addObject:[message substringWithRange:NSMakeRange(atRange.location, 1)]];
  332. NSString *str=[message substringFromIndex:atRange.location+1];
  333. [self getImageRange:str array:array];
  334. }
  335. }else if (message != nil) {
  336. [array addObject:message];
  337. }
  338. }
  339. else if (range.length>0 && range1.length>0 && range1.location < range.location){
  340. NSString *str = [message substringToIndex:range1.location + 1];
  341. NSString *str1 = [message substringFromIndex:range1.location + 1];
  342. [array addObject:str];
  343. [self getImageRange:str1 array:array];
  344. }
  345. else if (message != nil) {
  346. [array addObject:message];
  347. }
  348. }
  349. @end