// // YOUPAILZLiveChatMessageCell.m // TIANYAN // // Created by CY on 2021/5/11. // Copyright © 2021 leo. All rights reserved. // #import "YOUPAILZLiveChatMessageCell.h" #import "NIMKitUtil.h" #import "YOUPAILCGiftAttachment.h" #import "YOUPAILZGuardAttachment.h" #import "YOUPAILZLiveFollowAttachment.h" #import "YOUPAILZLiveManageAttachment.h" #import "SNKNinePatchImageView.h" #import "YOUPAILZLiveManagerListUtils.h" #import "YOUPAILZLevelView.h" #import "YOUPAIChatRoomManagerTagView.h" #import "YOUPAINIMInputEmoticonParser.h" #import "YOUPAINIMInputEmoticonManager.h" #import "UIImage+NIMKit.h" @interface YOUPAILZLiveChatMessageCell () @property (nonatomic,weak) SNKNinePatchImageView *youpaipbubbleImgV; @property (nonatomic, weak) UILabel *youpaipcontentL; //@property (nonatomic, weak) UIImageView *youpaipmanageImgV; @property (nonatomic, weak) UIImageView *youpaipvipImgV; @property (nonatomic, weak) UIImageView *youpaipboardImgV; @property (nonatomic, weak) UIImageView *youpaipbadgeImgV; @property (nonatomic, weak) YOUPAILZLevelView *youpaiplevelView; @property (nonatomic, weak) YOUPAIChatRoomManagerTagView *youpaipmanagerTagView; @property (nonatomic, strong) NSIndexPath *youpaipindexPath; @property (nonatomic, assign) NSInteger youpaiptagCount; @end @implementation YOUPAILZLiveChatMessageCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.transform = CGAffineTransformMakeScale(1, -1); self.backgroundColor = [UIColor clearColor]; [self youpaifinitUI]; } return self; } - (void)youpaifinitUI{ SNKNinePatchImageView *youpaipbubbleImgV = [SNKNinePatchImageView new]; // youpaipbubbleImgV.imageScale = 2; youpaipbubbleImgV.userInteractionEnabled = YES; [self.contentView addSubview:youpaipbubbleImgV]; self.youpaipbubbleImgV = youpaipbubbleImgV; UITapGestureRecognizer* tapGes =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaiftapOneAct)]; tapGes.numberOfTapsRequired = 1; [youpaipbubbleImgV addGestureRecognizer:tapGes]; [youpaipbubbleImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(0.0f); make.top.offset(6.0f); make.bottom.offset(0.0f); make.height.greaterThanOrEqualTo(38.0f); }]; UILabel *contentL = [[UILabel alloc] init]; contentL.textColor = [UIColor whiteColor]; contentL.font = LCFont12; contentL.numberOfLines = 0; [self.contentView addSubview:contentL]; self.youpaipcontentL = contentL; contentL.userInteractionEnabled = NO; [contentL mas_makeConstraints:^(MASConstraintMaker *make) { make.width.lessThanOrEqualTo(226.0f); }]; [youpaipbubbleImgV addConstraintsWithPaddingView:contentL]; YOUPAIChatRoomManagerTagView *youpaipmanagerTagView = [[YOUPAIChatRoomManagerTagView alloc] init]; [self.youpaipcontentL addSubview:youpaipmanagerTagView]; self.youpaipmanagerTagView = youpaipmanagerTagView; UIImageView *youpaipvipImgV = [[UIImageView alloc] init]; [self.youpaipcontentL addSubview:youpaipvipImgV]; self.youpaipvipImgV = youpaipvipImgV; UIImageView *youpaipboardImgV = [[UIImageView alloc] init]; [self.youpaipcontentL addSubview:youpaipboardImgV]; self.youpaipboardImgV = youpaipboardImgV; UIImageView *youpaipbadgeImgV = [[UIImageView alloc] init]; [self.youpaipcontentL addSubview:youpaipbadgeImgV]; self.youpaipbadgeImgV = youpaipbadgeImgV; YOUPAILZLevelView *youpaiplevelView = [[YOUPAILZLevelView alloc] init]; [self.youpaipcontentL addSubview:youpaiplevelView]; self.youpaiplevelView = youpaiplevelView; } - (void)youpaifreloadWithModel:(NIMMessage *)message font:(nonnull UIFont *)font indexPath:(nonnull NSIndexPath *)indexPath{ self.youpaipindexPath = indexPath; self.youpaipcontentL.font = font; NSString *giftImgStr = @""; self.userInteractionEnabled = YES; if(message.from == nil){ self.youpaipcontentL.textColor = HexColorFromRGB(0xFF3065); NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:message.text]; attributedString1 = [self youpaifhandleUserLevelWithFrom:message.from attributedStr:attributedString1 sendGiftHide:1]; self.youpaipcontentL.attributedText = attributedString1; self.userInteractionEnabled = NO; }else if (message.messageType == NIMMessageTypeText) { self.youpaipcontentL.textColor = [UIColor whiteColor]; NSString *nick = [NIMKitUtil showNick:message.from inMessage:message]; NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@:%@",nick,message.text]]; attributedString1 = [self youpaifhandleUserLevelWithFrom:message.from attributedStr:attributedString1 sendGiftHide:0]; [attributedString1 addAttributes:@{NSForegroundColorAttributeName:HexColorFromRGB(0xFFDD8D)} range:NSMakeRange(self.youpaiptagCount, nick.length + 2)]; self.youpaipcontentL.attributedText = attributedString1; }else if (message.session.sessionType == NIMSessionTypeChatroom && message.messageType == NIMMessageTypeNotification){ NIMNotificationObject *object = message.messageObject; NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)object.content; NSMutableArray *targetNicks = [[NSMutableArray alloc] init]; for (NIMChatroomNotificationMember *memebr in content.targets) { [targetNicks addObject:memebr.nick]; message.from = memebr.userId; } NSString *text = @""; NSString *targetText = [targetNicks componentsJoinedByString:@","]; switch (content.eventType) { case NIMChatroomEventTypeEnter: //成员进入聊天室 text = [NSString stringWithFormat:@" %@ 来了!",targetText]; break; case NIMChatroomEventTypeExit: //成员离开聊天室 text = [NSString stringWithFormat:@" %@ 离开了直播间",targetText]; break; // case NIMChatroomEventTypeAddBlack: //成员被拉黑 // // break; // case NIMChatroomEventTypeRemoveBlack: //成员被取消拉黑 // // break; // case NIMChatroomEventTypeAddMute://成员被设置禁言 // // break; // case NIMChatroomEventTypeRemoveMute://成员被取消禁言 // // break; // case NIMChatroomEventTypeAddManager://设置为管理员 // // break; // case NIMChatroomEventTypeRemoveManager://移除管理员 // // break; case NIMChatroomEventTypeAddMute://成员被禁言 text = [NSString stringWithFormat:@" %@ 被禁言了",targetText]; break; case NIMChatroomEventTypeRemoveMute://成员被取消禁言 text = [NSString stringWithFormat:@" %@ 被取消了禁言",targetText]; break; case NIMChatroomEventTypeAddManager://设置为管理员 text = [NSString stringWithFormat:@" %@ 被设置为管理员",targetText]; break; case NIMChatroomEventTypeRemoveManager://移除管理员 text = [NSString stringWithFormat:@" %@ 被取消设置为管理员",targetText]; break; case NIMChatroomEventTypeAddMuteTemporarily://聊天室成员被临时禁言 text = [NSString stringWithFormat:@" %@ 被禁言了",targetText]; break; case NIMChatroomEventTypeRemoveMuteTemporarily://聊天室成员被解除临时禁言 text = [NSString stringWithFormat:@" %@ 被取消了禁言",targetText]; break; default: break; } if (text.length != 0) { self.youpaipcontentL.textColor = HexColorFromRGB(0xFFDD8D); NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text]; attributedString1 = [self youpaifhandleUserLevelWithFrom:message.from attributedStr:attributedString1 sendGiftHide:0]; [attributedString1 addAttributes:@{NSForegroundColorAttributeName:HexColorFromRGB(0xFFDD8D)} range:NSMakeRange(self.youpaiptagCount, targetText.length + 1)]; self.youpaipcontentL.attributedText = attributedString1; } }else if(message.messageType == NIMMessageTypeCustom){ // 自定义消息 NIMCustomObject *object = (NIMCustomObject*)message.messageObject; NSInteger sendGiftHide = 0; NSString *nick = [NIMKitUtil showNick:message.from inMessage:message]; NSString *text = [NSString stringWithFormat:@"%@",nick]; if ([object.attachment isKindOfClass:[YOUPAILCGiftAttachment class]]){ // 礼物 YOUPAILCGiftAttachment* attachment = (YOUPAILCGiftAttachment*)object.attachment; nick = attachment.from_nickname; sendGiftHide = attachment.send_gift_hide; if (sendGiftHide == 1) { self.userInteractionEnabled = NO; } text = [NSString stringWithFormat:@"%@",nick]; text = [NSString stringWithFormat:@" %@ 送了%@个%@",text,@(attachment.gift_count),attachment.gift_name]; giftImgStr = attachment.gift_url; } if ([object.attachment isKindOfClass:[YOUPAILZGuardAttachment class]]){ // 守护 YOUPAILZGuardAttachment* attachment = (YOUPAILZGuardAttachment*)object.attachment; text = [NSString stringWithFormat:@" %@ 送了%@个%@",text,@(attachment.guard_count),attachment.guard_name]; giftImgStr = attachment.guard_url; } if ([object.attachment isKindOfClass:[YOUPAILZLiveFollowAttachment class]]) { // 关注 YOUPAILZLiveFollowAttachment* attachment = (YOUPAILZLiveFollowAttachment*)object.attachment; nick = @""; text = [NSString stringWithFormat:@" %@",attachment.msg]; } if ([object.attachment isKindOfClass:[YOUPAILZLiveManageAttachment class]]) { // 管理 YOUPAILZLiveManageAttachment* attachment = (YOUPAILZLiveManageAttachment*)object.attachment; text = [NSString stringWithFormat:@" %@",attachment.des]; } self.youpaipcontentL.textColor = HexColorFromRGB(0xFFDD8D); NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text]; attributedString1 = [self youpaifhandleUserLevelWithFrom:message.from attributedStr:attributedString1 sendGiftHide:sendGiftHide]; [attributedString1 addAttributes:@{NSForegroundColorAttributeName:HexColorFromRGB(0xFFDD8D)} range:NSMakeRange(self.youpaiptagCount, nick.length + 1)]; self.youpaipcontentL.attributedText = attributedString1; } self.youpaipcontentL.lineBreakMode = NSLineBreakByCharWrapping; } - (void)youpaifreloadWithAttachment:(YOUPAILZChatRoomGiftAttachment *)attachment font:(UIFont *)font indexPath:(NSIndexPath *)indexPath{ self.youpaipindexPath = indexPath; self.youpaipcontentL.font = font; self.userInteractionEnabled = YES; NSInteger sendGiftHide = attachment.send_gift_hide; if (sendGiftHide == 1) { self.userInteractionEnabled = NO; } NSString *text = [NSString stringWithFormat:@" %@ 送给 %@ %@个%@",attachment.from_nickname,attachment.to_nickname,@(attachment.gift_count),attachment.gift_name]; self.youpaipcontentL.textColor = HexColorFromRGB(0xFFDD8D); NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:text]; attributedString1 = [self youpaifhandleUserLevelWithFrom:[NSString stringWithFormat:@"%@",@(attachment.from_uid)] attributedStr:attributedString1 sendGiftHide:sendGiftHide]; self.youpaipcontentL.attributedText = attributedString1; self.youpaipcontentL.lineBreakMode = NSLineBreakByCharWrapping; } - (NSMutableAttributedString *)youpaifhandleUserLevelWithFrom:(NSString *)from attributedStr:(NSMutableAttributedString *)attributedStr sendGiftHide:(NSInteger)sendGiftHide{ NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; self.youpaipmanagerTagView.hidden = YES; self.youpaipvipImgV.hidden = YES; self.youpaipboardImgV.hidden = YES; self.youpaipbadgeImgV.hidden = YES; self.youpaiplevelView.hidden = YES; if (sendGiftHide == 0) { CGFloat width = 0.0f; UIView *frontView = nil; self.youpaipmanagerTagView.hidden = NO; self.youpaipvipImgV.hidden = NO; self.youpaipboardImgV.hidden = NO; self.youpaipbadgeImgV.hidden = NO; self.youpaiplevelView.hidden = NO; NSString *ext = [self youpaifgetExtByUserId:from]; NSInteger grade = 0; NSInteger vipLevel = 0; NSString *badge = @""; if (ext.length != 0) { NSData *data = [ext dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; if ([dict.allKeys containsObject:@"grade"]) { grade = [dict integerValueForKey:@"grade" defaultValue:0]; } if ([dict.allKeys containsObject:@"bubble"]) { NSString *bubble = [NSString stringWithFormat:@"%@",[dict stringValueForKey:@"bubble" defaultValue:@""]]; if (bubble.length != 0) { self.youpaipbubbleImgV.ninePatchImage = [SNKNinePatchImage ninePatchImageWithImageData:[LCSaveData getBubbleWithKey:[LCTools getImageUrlWithAddress:bubble].absoluteString] scale:2]; } } if ([dict.allKeys containsObject:@"vip"]) { vipLevel = [dict integerValueForKey:@"vip" defaultValue:0]; } if ([dict.allKeys containsObject:@"badge"]) { badge = [NSString stringWithFormat:@"%@",[dict stringValueForKey:@"badge" defaultValue:@""]]; } } NSMutableArray *textAttachments = [NSMutableArray array]; /// 管理员 BOOL hasManage = NO; if ([[YOUPAILZChatRoomManager shareManager].chatroomModel.youpaipmanagers.allKeys containsObject:from]) { hasManage = YES; } if (hasManage) { self.youpaipmanagerTagView.hidden = NO; [self.youpaipmanagerTagView youpaifreloadWithModel:[YOUPAILZChatroomManagerTagModel mj_objectWithKeyValues:[[YOUPAILZChatRoomManager shareManager].chatroomModel.youpaipmanagers objectForKey:from]]]; self.youpaipmanagerTagView.mj_x = width; width += self.youpaipmanagerTagView.mj_w; frontView = self.youpaipmanagerTagView; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, self.youpaipmanagerTagView.mj_w, 14.0f); [textAttachments addObject:attch]; }else{ self.youpaipmanagerTagView.hidden = YES; } if (frontView != nil) { width += 3.0f; frontView = nil; } /// 贵族 NSDictionary *vipInfo = [LCSaveData getNobleInfo]; BOOL hasVip = NO; if ([vipInfo.allKeys containsObject:[NSString stringWithFormat:@"%@",@(vipLevel)]]) { hasVip = YES; } if (hasVip) { NSDictionary *vipItemInfo = [vipInfo objectForKey:[NSString stringWithFormat:@"%@",@(vipLevel)]]; self.youpaipvipImgV.hidden = NO; [self.youpaipvipImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:[vipItemInfo stringValueForKey:@"mini_icon" defaultValue:@""]]]; self.youpaipvipImgV.frame = CGRectMake(width, -1.0f, 30.0f, 19.0f); width += 30.0f; frontView = self.youpaipvipImgV; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, self.youpaipvipImgV.mj_w, 14.0f); [textAttachments addObject:attch]; }else{ self.youpaipvipImgV.hidden = YES; } if (frontView != nil) { width += 3.0f; frontView = nil; } /// 排行 NSDictionary *rankTopThere = [LCSaveData getLiveRankTopThere]; BOOL hasRank = NO; if ([rankTopThere.allKeys containsObject:from]) { hasRank = YES; } if (hasRank) { NSDictionary *rankInfo = [rankTopThere objectForKey:from]; self.youpaipboardImgV.hidden = NO; [self.youpaipboardImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:[rankInfo objectForKey:@"img"]]]; self.youpaipboardImgV.frame = CGRectMake(width, 0.0f, 21.0f, 18.0f); width += 21.0f; frontView = self.youpaipboardImgV; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, self.youpaipboardImgV.mj_w, 14.0f); [textAttachments addObject:attch]; }else{ self.youpaipboardImgV.hidden = YES; } if (frontView != nil) { width += 3.0f; frontView = nil; } /// 勋章 NSDictionary *badgeInfo = [LCSaveData getBadgeInfo]; BOOL hasBadge = NO; if (badge.length != 0) { hasBadge = YES; } if (hasBadge) { NSDictionary *badgeItemInfo = [badgeInfo objectForKey:badge]; self.youpaipbadgeImgV.hidden = NO; [self.youpaipbadgeImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:[badgeItemInfo stringValueForKey:@"file" defaultValue:@""]]]; CGFloat badgeIconWidth = [badgeItemInfo floatValueForKey:@"width" defaultValue:0]; self.youpaipbadgeImgV.frame = CGRectMake(width, -0.0f, badgeIconWidth, 17.0f); width += badgeIconWidth; frontView = self.youpaipbadgeImgV; NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, self.youpaipbadgeImgV.mj_w, 14.0f); [textAttachments addObject:attch]; }else{ self.youpaipbadgeImgV.hidden = YES; } if (frontView != nil) { width += 3.0f; frontView = nil; } /// 等级 NSDictionary *levelInfo = [LCSaveData getLevelInfo]?:@{}; NSArray *grades = [levelInfo arrayValueForKey:@"grade" defaultValue:@[]]; NSDictionary *levelDict = [levelInfo dictionaryValueForKey:@"info" defaultValue:@{}]; NSString *levelName = [grades objectAtIndex:grade]; NSDictionary *levelItem = [levelDict objectForKey:levelName]; NSString *levelIcon = [levelItem stringValueForKey:@"img1" defaultValue:@""]; CGFloat levelIconWidth = [levelItem floatValueForKey:@"icon_width" defaultValue:0]; NSString *levelBg = [levelItem stringValueForKey:@"img2" defaultValue:@""]; CGFloat levelBgWidth = [levelItem floatValueForKey:@"icon1_width" defaultValue:0]; [self.youpaiplevelView youpaifreloadWithIcon:levelIcon iconWidth:levelIconWidth bgImg:levelBg bgImgWidth:levelBgWidth levelName:levelName level:grade]; self.youpaiplevelView.mj_y = -0.0f; self.youpaiplevelView.mj_x = width; width += self.youpaiplevelView.mj_w; if (width >= 246.0f) { self.youpaiplevelView.mj_y = -0.0f + 14.0f + 4.0f; self.youpaiplevelView.mj_x = 0.0f; } NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, self.youpaiplevelView.mj_w, 14.0f); [textAttachments addObject:attch]; if (self.youpaipbubbleImgV.ninePatchImage == nil) { self.youpaipbubbleImgV.ninePatchImage = [SNKNinePatchImage ninePatchImageWithImageData:[LCSaveData getBubbleWithKey:[LCTools getImageUrlWithAddress:@"/v1.3/dress/bubble/lALPD3zUOqg7eJA7bg_110_59.png"].absoluteString] scale:2]; } for (NSInteger i = 0; i < textAttachments.count; i ++) { NSTextAttachment *textAttachment = textAttachments[i]; NSAttributedString *attr = [NSAttributedString attributedStringWithAttachment:textAttachment]; [attributedStr insertAttributedString:attr atIndex:i]; } { NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = [UIImage new]; attch.bounds = CGRectMake(0.0f, 0.0f, 5.0f, 14.0f); [textAttachments addObject:attch]; NSAttributedString *attr = [NSAttributedString attributedStringWithAttachment:attch]; [attributedStr insertAttributedString:attr atIndex:textAttachments.count]; } self.youpaiptagCount = textAttachments.count; }else{ self.youpaipbubbleImgV.ninePatchImage = [SNKNinePatchImage ninePatchImageWithImageData:[LCSaveData getBubbleWithKey:[LCTools getImageUrlWithAddress:@"/v1.3/dress/bubble/lALPD3zUOqg7eJA7bg_110_59.png"].absoluteString] scale:2]; } paragraphStyle1.alignment = NSTextAlignmentJustified; paragraphStyle1.lineSpacing = 3.0f; [attributedStr addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle1} range:NSMakeRange(0, attributedStr.string.length)]; [self youpaifHandleAmojiForText:attributedStr]; return attributedStr; } - (void)youpaifHandleAmojiForText:(NSMutableAttributedString *)attributedStr{ NSArray *tokens = [[YOUPAINIMInputEmoticonParser currentParser] tokens:attributedStr.string]; for (NIMInputTextToken *token in tokens) { if (token.type == NIMInputTokenTypeEmoticon) { NIMInputEmoticon *emoticon = [[YOUPAINIMInputEmoticonManager sharedManager] emoticonByTag:token.text]; UIImage *image = [UIImage nim_emoticonInKit:emoticon.filename]; if (image) { NSTextAttachment *attch = [[NSTextAttachment alloc] init]; attch.image = image; attch.bounds = CGRectMake(0.0f, 0.0f, 16.0f, 16.0f); NSRange range = [attributedStr.string rangeOfString:token.text]; NSAttributedString *attr = [NSAttributedString attributedStringWithAttachment:attch]; [attributedStr deleteCharactersInRange:range]; [attributedStr insertAttributedString:attr atIndex:range.location]; } } } } - (NSString *)youpaifgetExtByUserId:(NSString *)userId{ NIMUser *nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:userId]; return nimUser.userInfo.ext; } - (void)youpaiftapOneAct{ if(self.showUserInfoBlock != nil){ self.showUserInfoBlock(self.youpaipindexPath); } } - (void)youpaiftapTwoAct{ if(self.youpaipreplyBlock != nil){ self.youpaipreplyBlock(self.youpaipindexPath); } } @end