123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- //
- // JXMessageCell.m
- // shiku_im
- //
- // Created by Apple on 16/10/10.
- // Copyright © 2016年 Reese. All rights reserved.
- //
- static CGFloat kuangkkHHH;
- #import "JXMessageCell.h"
- #import "emojiViewController.h"
- #import "EmojiTextAttachment.h"
- //#define TEXT_MAX_HEIGHT 500.0f
- @implementation JXMessageCell
- - (void)awakeFromNib {
- [super awakeFromNib];
- // self.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, 55);
- kuangkkHHH=0;
- }
- - (void)dealloc {
-
- [self.readDelTimer invalidate];
- self.readDelTimer = nil;
- }
- -(void)creatUI{
- _messageConent=[[JXEmoji alloc] init];
- _messageConent.userInteractionEnabled = YES;
- _messageConent.lineBreakMode = NSLineBreakByWordWrapping;
- _messageConent.numberOfLines = 0;
- _messageConent.backgroundColor = [UIColor clearColor];
-
- [self.bubbleBg addSubview:_messageConent];
- _timeIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
- _timeIndexLabel.layer.cornerRadius = _timeIndexLabel.frame.size.width / 2;
- _timeIndexLabel.layer.masksToBounds = YES;
- _timeIndexLabel.textColor = [UIColor whiteColor];
- _timeIndexLabel.backgroundColor = HEXCOLOR(0x02d8c9);
- _timeIndexLabel.textAlignment = NSTextAlignmentCenter;
- _timeIndexLabel.text = @"0";
- _timeIndexLabel.font = [UIFont systemFontOfSize:12.0];
- _timeIndexLabel.hidden = YES;
- [self.contentView addSubview:_timeIndexLabel];
- UITapGestureRecognizer * tapPress = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dotapPress:)];
- [self.contentView addGestureRecognizer:tapPress];
- }
- - (void)dotapPress:(UITapGestureRecognizer *)tap {
- if (self.block) {
- self.block(self.msg);
- }
- }
- -(void)setCellData{
- [super setCellData];
- JLBubbleManager *bubbleManager;
- if (self.msg.isGroup) {
- memberData *data = [[memberData alloc] init];
- data.roomId = self.room.roomId;
- data = [data getCardNameById:self.msg.fromUserId];
- if (data.bubbleFontId.length <= 0) {
- bubbleManager = [JLBubbleManager sharedManager];
- bubbleManager.id = @"0";
- }else {
- bubbleManager = [[JLBubbleManager sharedManager] fechGradeByBubbleFontId:data.bubbleFontId];
- }
-
- // NSLog(@"ID--%@---fromUserId%@--%@", data.bubbleFontId,self.msg.fromUserId,bubbleManager);
-
- }else {
- // 单聊
- // 从朋友表里获取
- JXUserObject *user;
- if (self.msg.isMySend) {
- user = [[JXUserObject sharedInstance] getUserById:MY_USER_ID];
- }else {
- user = [[JXUserObject sharedInstance] getUserById:self.msg.fromUserId];
- }
- bubbleManager = [[JLBubbleManager sharedManager] fechGradeByBubbleFontId:user.bubbleFontId];
- }
-
-
-
- if (bubbleManager == nil) {
- _messageConent.font = [[JLBubbleManager sharedManager] getBubbleFontNameIndex:-1];
- _messageConent.textColor = [[JLBubbleManager sharedManager] getBubbleFontColor:@""];
- }else {
- _messageConent.font = [[JLBubbleManager sharedManager] getBubbleFontNameIndex:[bubbleManager.code intValue]];
- _messageConent.textColor = [[JLBubbleManager sharedManager] getBubbleFontColor:bubbleManager.color];
- }
- _messageConent.textColor =[UIColor blackColor];
- // 设置显示字体
- _messageConent.frame = CGRectMake(0, 0, 200, 20);
- if (self.msg.objectId.length > 0) {
- _messageConent.atUserIdS = self.msg.objectId;
- }
- if ([self.msg.isReadDel boolValue] && ([self.msg.fileName length] <= 0 || [self.msg.fileName intValue] <= 0) && !self.msg.isMySend) {
- _messageConent.userInteractionEnabled = NO;
- _messageConent.text = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
- // _messageConent.textColor = HEXCOLOR(0x0079FF);
- _timeIndexLabel.hidden = YES;
- }else {
- _messageConent.userInteractionEnabled = YES;
- // _messageConent.textColor = HEXCOLOR(0x333333);
- // _messageConent.textColor = [UIColor whiteColor];
- if ([self.msg.content containsString:@"===jixin"]) {
-
- NSArray *typeArr = [self.msg.content componentsSeparatedByString:@"===jixin"];
-
- _messageConent.text=[typeArr firstObject];
- }else{
- _messageConent.text = self.msg.content;
- }
-
-
-
- // NSLog(@"000000%@",self.msg.content);
-
-
- _timeIndexLabel.hidden = YES;
-
- if (!self.msg.isMySend && [self.msg.fileName isKindOfClass:[NSString class]] && [self.msg.fileName length] > 0 && [self.msg.fileName intValue] > 0) {
- self.timeIndexLabel.hidden = NO;
-
- NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
- NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
- NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
-
- NSMutableAttributedString * textC = [self changeEmjoyText:messageText];
- [textC addAttribute:NSFontAttributeName value:SYSFONT(g_constant.chatFont) range:NSMakeRange(0, textC.length)];
- CGSize size = [textC boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
-
-
- NSInteger count = size.height / _messageConent.font.lineHeight;
- NSLog(@"countcount === %ld-----%f-----%@",count,[[NSDate date] timeIntervalSince1970],self.msg.fileName);
- // NSLog(@"countcount === %ld,,,,%f,,,,%@",count,[[NSDate date] timeIntervalSince1970], self.msg.fileName);
- count = count * 10 - ([[NSDate date] timeIntervalSince1970] - [self.msg.fileName longLongValue]);
- self.timerIndex = count;
-
- NSLog(@"countcount1 === %ld",count);
- if (count > 0) {
- self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",count];
- if (!self.readDelTimer) {
- self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
- }
- }else {
-
- self.msg.fileName = @"0";
-
- //阅后即焚通知
- [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
- [self deleteMsg:self.msg];
- }
-
-
- }
- }
- [self creatBubbleBg];
- }
- -(void)creatBubbleBg{
- CGSize textSize = _messageConent.frame.size;
- int n = textSize.width;
- if ([_messageConent.text containsString:@"--【"]) {
- kuangkkHHH=50.0;
- }
-
- //聊天长度反正就是算错了,强行改
-
- int inset = 3;
- if(self.msg.isMySend){
- self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)-INSETS*2-inset*2-n-CHAT_WIDTH_ICON, INSETS,_kuangHHH+ n+INSETS*2+inset*2, textSize.height+INSETS*2);
- self.bubbleBgImageView.frame = CGRectMake(0, 0,n+INSETS*2+inset*2, textSize.height+INSETS*2+INSETS);
- [_messageConent setFrame:CGRectMake(INSETS*0.4 + 3 +5, 10,_kuangHHH+ n + 5, textSize.height)];
-
- // [_messageConent setFrame:CGRectMake(INSETS*0.4 + 3, INSETS, n + 5, textSize.height+50)];
- _timeIndexLabel.frame = CGRectMake(self.bubbleBg.frame.origin.x - 30, self.bubbleBg.frame.origin.y, 20, 20);
- }else
- {
-
-
- self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup)+9, n+INSETS*2+inset*2, textSize.height+INSETS*2);
-
- self.bubbleBgImageView.frame =CGRectMake(0, 2, n+INSETS*2+inset*2, textSize.height+INSETS*2+20);
-
- [_messageConent setFrame:CGRectMake(INSETS + 3, 10, n+5, textSize.height)];
- _timeIndexLabel.frame = CGRectMake(CGRectGetMaxX(self.bubbleBg.frame) + 10, self.bubbleBg.frame.origin.y, 20, 20);
- // _messageConent.textAlignment = NSTextAlignmentLeft;
- }
- if (self.msg.isShowTime) {
- CGRect frame = self.bubbleBg.frame;
- frame.origin.y = self.bubbleBg.frame.origin.y + 40;
- self.bubbleBg.frame = frame;
-
- _timeIndexLabel.frame = CGRectMake(_timeIndexLabel.frame.origin.x, self.bubbleBg.frame.origin.y, 20, 20);
- }
-
- }
- - (void)setBackgroundImage {
- [super setBackgroundImage];
- 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) {
- self.isDidMsgCell = YES;
- }
- if ([self.msg.isReadDel boolValue] && !self.msg.isMySend && self.isDidMsgCell) {
- [self drawReadDelView:YES];
- self.isDidMsgCell = NO;
- }
- }
- //复制信息到剪贴板
- - (void)myCopy{
-
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- [pasteboard setString:self.msg.content];
- }
- + (float)getChatCellHeight:(JXMessageObject *)msg {
-
-
- if ([msg.chatMsgHeight floatValue] > 1) {
- CGFloat chatMsgHeight = [msg.chatMsgHeight floatValue]+kuangkkHHH;
-
- return chatMsgHeight;
- }
- float n = 0.0;
- JXEmoji *messageConent=[[JXEmoji alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
- messageConent.backgroundColor = [UIColor clearColor];
- // messageConent.userInteractionEnabled = NO;
- messageConent.numberOfLines = 0;
- messageConent.lineBreakMode = NSLineBreakByWordWrapping;//UILineBreakModeWordWrap;
-
- JLBubbleManager *bubbleManager;
- if (msg.isGroup) {
- memberData *data = [[memberData alloc] init];
- data.roomId = msg.toId;
- data = [data getCardNameById:msg.fromUserId];
- if (data.bubbleFontId.length <= 0) {
- bubbleManager = [JLBubbleManager sharedManager];
- bubbleManager.id = @"0";
- }else {
- bubbleManager = [[JLBubbleManager sharedManager] fechGradeByBubbleFontId:data.bubbleFontId];
- }
-
- // NSLog(@"ID--%@---fromUserId%@--%@", data.bubbleFontId,self.msg.fromUserId,bubbleManager);
-
- }else {
- // 单聊
- // 从朋友表里获取
- JXUserObject *user;
- if (msg.isMySend) {
- user = [[JXUserObject sharedInstance] getUserById:MY_USER_ID];
- }else {
- user = [[JXUserObject sharedInstance] getUserById:msg.fromUserId];
- }
- bubbleManager = [[JLBubbleManager sharedManager] fechGradeByBubbleFontId:user.bubbleFontId];
- }
-
- if (bubbleManager == nil) {
- messageConent.font = [[JLBubbleManager sharedManager] getBubbleFontNameIndex:-1];
- messageConent.textColor = [[JLBubbleManager sharedManager] getBubbleFontColor:@""];
- }else {
- messageConent.font = [[JLBubbleManager sharedManager] getBubbleFontNameIndex:[bubbleManager.code intValue]];
- messageConent.textColor = [[JLBubbleManager sharedManager] getBubbleFontColor:bubbleManager.color];
- }
- messageConent.offset = -12;
-
- messageConent.frame = CGRectMake(0, 0, 200, 20);
- if ([msg.isReadDel boolValue] && [msg.fileName intValue] <= 0 && !msg.isMySend) {
- messageConent.text = [NSString stringWithFormat:@"%@ T", Localized(@"JX_ClickAndView")];
- }else {
-
- messageConent.text = msg.content;
-
- }
-
- if (msg.isGroup && !msg.isMySend) {
- n = messageConent.frame.size.height+10*3 + 27;
- if (msg.isShowTime) {
- n=messageConent.frame.size.height+10*3 + 40 + 20;
- }
- n += GROUP_CHAT_INSET;
- }else {
- n= messageConent.frame.size.height+10*3 + 10;
- if (msg.isShowTime) {
- n=messageConent.frame.size.height+10*3 + 40 + 10;
- }
- }
-
- // NSLog(@"heightForRowAtIndexPath_%d,%d:=%@",indexPath.row,n,_messageConent.text);
- if(n<55)
- n = 55+10;
- if (msg.isShowTime) {
- if(n<95)
- n = 95+10;
- }
- msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
- if ([msg.content containsString:@"===jixinJXBigRellV"]){
- msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n]; //--20
- }
- if (!msg.isNotUpdateHeight) {
- [msg updateChatMsgHeight];
- }
- // NSLog(@"高度%f--内容高度%f--内容%@", n, messageConent.frame.size.height,msg.content);
- return n;
- }
- -(void)didTouch:(UIButton*)button{
- if ([self.msg.isReadDel boolValue] && [self.msg.fileName intValue] <= 0 && !self.msg.isMySend) {
- [self.msg sendAlreadyReadMsg];
-
- self.msg.fileName = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
- [self.msg updateFileName];
-
- self.timeIndexLabel.hidden = NO;
-
- _messageConent.text = self.msg.content;
-
- // NSString *messageR = [self.msg.content stringByReplacingOccurrencesOfString:@"\r" withString:@""]; //去掉回车键
- // NSString *messageN = [messageR stringByReplacingOccurrencesOfString:@"\n" withString:@""]; //去掉回车键
- // NSString *messageText = [messageN stringByReplacingOccurrencesOfString:@" " withString:@""]; //去掉空格
- // CGSize size = [messageText boundingRectWithSize:CGSizeMake(_messageConent.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(g_constant.chatFont)} context:nil].size;
- // NSInteger count = size.height / _messageConent.font.lineHeight;
- // self.msg.fileName = [NSString stringWithFormat:@"%ld", count * 10];
- self.isDidMsgCell = YES;
- self.msg.chatMsgHeight = [NSString stringWithFormat:@"0"];
- [self.msg updateChatMsgHeight];
- [g_notify postNotificationName:kCellMessageReadDelNotifaction object:[NSNumber numberWithInt:self.indexNum]];
- // self.readDelTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
-
- }
- }
- - (void)timerAction:(NSTimer *)timer {
-
- if (self.timerIndex <= 0) {
- [self.readDelTimer invalidate];
- self.readDelTimer = nil;
- self.msg.fileName = @"0";
-
- //阅后即焚通知
- [g_notify postNotificationName:kCellReadDelNotification object:self.msg];
- [self deleteMsg:self.msg];
- return;
- }
- self.timeIndexLabel.text = [NSString stringWithFormat:@"%ld",-- self.timerIndex];
- // self.msg.fileName = self.timeIndexLabel.text;
- // [self.msg updateFileName];
-
- }
- - (void)deleteMsg:(JXMessageObject *)msg{
-
- if ([self.msg.isReadDel boolValue]) {
-
- if ([self.msg.fileName intValue] > 0) {
- return;
- }
-
- //渐变隐藏
- [UIView animateWithDuration:2.f animations:^{
- self.bubbleBg.alpha = 0;
- self.timeIndexLabel.alpha = 0;
- self.readImage.alpha = 0;
- self.burnImage.alpha = 0;
- } completion:^(BOOL finished) {
- //动画结束后删除UI
- [self.delegate performSelectorOnMainThread:self.readDele withObject:msg waitUntilDone:NO];
- self.bubbleBg.alpha = 1;
- self.timeIndexLabel.alpha = 1;
- self.readImage.alpha = 1;
- self.burnImage.alpha = 1;
- }];
- }
-
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- #pragma mark - 有表情的txt 转换成 含图片的str
- - (NSMutableAttributedString *)changeEmjoyText:(NSString *)text {
- NSMutableAttributedString *conStr = [[NSMutableAttributedString alloc] init];
- NSMutableArray *arr = [NSMutableArray array];
- [self getImageRange:text array:arr];
- if (arr.count > 1) {
- for (NSInteger i = 0; i < arr.count; i ++) {
- NSString *str = arr[i];
- NSInteger n;
-
- if ([str hasPrefix:@"["]&&[str hasSuffix:@"]"] && [g_faceVC.shortNameArrayE containsObject:str]) {
- n = [g_faceVC.shortNameArrayE indexOfObject:str];
- NSDictionary *dic = [g_constant.emojiArray objectAtIndex:n];
-
- EmojiTextAttachment *attachment = [[EmojiTextAttachment alloc] init];
- attachment.emojiTag = str;
- attachment.image = [UIImage imageNamed:dic[@"filename"]];
- attachment.bounds = CGRectMake(0, -4, 23, 23);
-
- [conStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
- }else {
-
- [conStr appendAttributedString:[[NSAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:self.messageConent.font}]];
- }
-
- }
- }else {
- return [[NSMutableAttributedString alloc] initWithString:text];
- }
- return conStr;
- }
- //将表情和文字分开,装进array
- -(void)getImageRange:(NSString*)message array: (NSMutableArray*)array {
- NSRange range=[message rangeOfString: @"["];
- NSRange range1=[message rangeOfString: @"]"];
- NSRange atRange = [message rangeOfString:@"@"];
- //判断当前字符串是否还有表情的标志。
- if (((range.length>0 && range1.length>0) || atRange.length>0) && range1.location > range.location) {
- if (range.length>0 && range1.length>0) {
- // self.contentEmoji = YES;
- // if (range.location > 0) {
- // [array addObject:[message substringToIndex:range.location]];
- // [array addObject:[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)]];
- // NSString *str=[message substringFromIndex:range1.location+1];
- // [self getImageRange:str array:array];
- // }else {
- // NSString *nextstr=[message substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
- // //排除文字是“”的
- // if (![nextstr isEqualToString:@""]) {
- // [array addObject:nextstr];
- // NSString *str=[message substringFromIndex:range1.location+1];
- // [self getImageRange:str array:array];
- // }else {
- // return;
- // }
- // }
- if (range.location > 0) {
- NSString *str = [message substringToIndex:range.location];
- NSString *str1 = [message substringFromIndex:range.location];
- [array addObject:str];
- [self getImageRange:str1 array:array];
- }else {
- NSString *emojiString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location - 1)];
- BOOL isEmoji = NO;
- NSString *str;
- NSString *str1;
- for (NSMutableDictionary *dic in g_constant.emojiArray) {
- NSString *emoji = [dic objectForKey:@"english"];
- if ([emoji isEqualToString:emojiString]) {
- isEmoji = YES;
- break;
- }
- }
- if (isEmoji) {
- str = [message substringWithRange:NSMakeRange(range.location, range1.location + 1)];
- str1 = [message substringFromIndex:range1.location + 1];
- [array addObject:str];
- }else{
- NSString *posString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location)];
- NSRange posRange = [posString rangeOfString:@"["];
- if (posRange.location != NSNotFound) {
- str = [message substringToIndex:posRange.location + 1];
- str1 = [message substringFromIndex:posRange.location + 1];
- [array addObject:str];
- }else{
- str = [message substringToIndex:range1.location + 1];
- str1 = [message substringFromIndex:range1.location + 1];
- [array addObject:str];
- }
- }
- [self getImageRange:str1 array:array];
- }
-
-
- } else if (atRange.length>0) {
- if (atRange.location > 0) {
- [array addObject:[message substringToIndex:atRange.location]];
- [array addObject:[message substringWithRange:NSMakeRange(atRange.location, 1)]];
- NSString *str=[message substringFromIndex:atRange.location+1];
- [self getImageRange:str array:array];
- }else{
- [array addObject:[message substringWithRange:NSMakeRange(atRange.location, 1)]];
- NSString *str=[message substringFromIndex:atRange.location+1];
- [self getImageRange:str array:array];
- }
-
- }else if (message != nil) {
- [array addObject:message];
- }
- }
- else if (range.length>0 && range1.length>0 && range1.location < range.location){
- NSString *str = [message substringToIndex:range1.location + 1];
- NSString *str1 = [message substringFromIndex:range1.location + 1];
- [array addObject:str];
- [self getImageRange:str1 array:array];
- }
- else if (message != nil) {
- [array addObject:message];
- }
- }
- @end
|