// // MOSystemNormalCellData.m // TUIConversation // // Created by SuperC on 2025/5/16. // #import "MOSystemNormalCellData.h" @implementation MOSystemNormalCellData + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message { NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil]; if (param == nil) { return nil; } MOSystemNormalCellData *cellData = [[MOSystemNormalCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming]; cellData.reuseId = @"MOSystemNormalCell"; cellData.innerMessage = message; cellData.type = [[MOSystemNormalCellData objectOrNilForKey:@"type" fromDictionary:param] integerValue]; cellData.img = [MOSystemNormalCellData objectOrNilForKey:@"img" fromDictionary:param]; cellData.title = [MOSystemNormalCellData objectOrNilForKey:@"title" fromDictionary:param]; cellData.content = [MOSystemNormalCellData objectOrNilForKey:@"content" fromDictionary:param]; cellData.link = [MOSystemNormalCellData objectOrNilForKey:@"link" fromDictionary:param]; cellData.linkStr = [MOSystemNormalCellData objectOrNilForKey:@"linkStr" fromDictionary:param]; return cellData; } + (NSString *)getDisplayString:(V2TIMMessage *)message { NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil]; if (param == nil) { return nil; } NSString *contentStr = [MOSystemNormalCellData objectOrNilForKey:@"content" fromDictionary:param]; if(contentStr.length > 0){ return contentStr; } return message.customElem.desc; } - (BOOL)showPicture { if (self.type == 1) {//图片.标题。内容.链接 return YES; } if (self.type == 2) {//图片.标题。内容 return YES; } else if (self.type == 3) {//图片.内容 return YES; } else if (self.type == 4) {//图片.内容。链接 return YES; } else if (self.type == 5) {//标题.内容。链接 return NO; } else if (self.type == 6) {//内容。链接 return NO; } else if (self.type == 7) {//标题,内容,文字调整链接 return NO; } else if (self.type == 8) {//内容,文字调整链接 return NO; } else if (self.type == 9) {//内容 return NO; } return NO; } - (BOOL)showTitle { if (self.type == 1) {//图片.标题。内容.链接 return YES; } if (self.type == 2) {//图片.标题。内容 return YES; } else if (self.type == 3) {//图片.内容 return NO; } else if (self.type == 4) {//图片.内容。链接 return NO; } else if (self.type == 5) {//标题.内容。链接 return YES; } else if (self.type == 6) {//内容。链接 return NO; } else if (self.type == 7) {//标题,内容,文字调整链接 return YES; } else if (self.type == 8) {//内容,文字调整链接 return NO; } else if (self.type == 9) {//内容 return NO; } return NO; } - (BOOL)showDetailsView { if (self.type == 1) {//图片.标题。内容.链接 return YES; } if (self.type == 2) {//图片.标题。内容 return NO; } else if (self.type == 3) {//图片.内容 return NO; } else if (self.type == 4) {//图片.内容。链接 return YES; } else if (self.type == 5) {//标题.内容。链接 return YES; } else if (self.type == 6) {//内容。链接 return YES; } else if (self.type == 7) {//标题,内容,文字调整链接 return NO; } else if (self.type == 8) {//内容,文字调整链接 return NO; } else if (self.type == 9) {//内容 return NO; } return NO; } - (CGSize)contentSize { //文字内容显示区域 CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0; CGFloat contentWidth = viewWidth - 10.0 * 2.0; //标题以上固定距离 CGFloat titleTopSpacing = 40.0; CGFloat pictureHeight = 0; if (self.img.length > 0 && [self showPicture]) { pictureHeight = 74 + 10;//10是图片底部的间距 } //标题高度 CGFloat titleHeight = 0; if (self.title.length > 0 && [self showTitle]) { UIFont *titleFont = [MOSystemNormalCellData getTheFontWithSize:15.0 AndFontName:@"Roboto-Bold"]; titleHeight = [self.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil].size.height + 2;//2是补偿高度 } //内容高度 CGFloat contentHeight = 0; if(self.content.length > 0){ UIFont *contentFont = [MOSystemNormalCellData getTheFontWithSize:13.0 AndFontName:@"Roboto"]; contentHeight = [self.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : contentFont} context:nil].size.height; } //跳转链接内容高度 CGFloat linkHeight = 0; if (self.link.length > 0 && [self showDetailsView]) { linkHeight = 30; } //30是bgView上下距离 CGFloat bgViewSpacing = 30; CGFloat totalHeigh = 0; if (self.type == 1) {//图片.标题。内容.链接 totalHeigh = titleTopSpacing + pictureHeight + titleHeight + contentHeight + linkHeight + bgViewSpacing; } if (self.type == 2) {//图片.标题。内容 totalHeigh = titleTopSpacing + titleHeight + pictureHeight + contentHeight + bgViewSpacing; } else if (self.type == 3) {//图片.内容 totalHeigh = titleTopSpacing + pictureHeight + contentHeight + bgViewSpacing; } else if (self.type == 4) {//图片.内容。链接 totalHeigh = titleTopSpacing + pictureHeight + contentHeight + linkHeight + bgViewSpacing; } else if (self.type == 5) {//标题.内容。链接 totalHeigh = titleTopSpacing + titleHeight + contentHeight + linkHeight + bgViewSpacing; } else if (self.type == 6) {//内容。链接 totalHeigh = titleTopSpacing + contentHeight + linkHeight + bgViewSpacing; } else if (self.type == 7) {//标题,内容,文字调整链接 totalHeigh = titleTopSpacing + titleHeight + contentHeight + bgViewSpacing; } else if (self.type == 8) {//内容,文字调整链接 totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing; } else if (self.type == 9) {//内容 totalHeigh = titleTopSpacing + contentHeight + bgViewSpacing - 5;//这个类型有5的偏差,直接这里改好了 } CGSize size = CGSizeMake(245, ceilf(totalHeigh)); return size; } + (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict { id object = [dict objectForKey:aKey]; return [object isEqual:[NSNull null]] ? nil : object; } + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{ UIFont *customFont = [UIFont fontWithName:fontName size:fontSize]; if(!customFont){ customFont = [MOSystemNormalCellData MODisplayFontWithSize:fontSize bold:YES itatic:NO weight:UIFontWeightMedium]; } return customFont; } + (UIFont *)MODisplayFontWithSize:(CGFloat)fontSize bold:(BOOL)bold itatic:(BOOL)italic weight:(UIFontWeight)weight { UIFont *font = [UIFont systemFontOfSize:fontSize weight:weight]; UIFontDescriptorSymbolicTraits symbolicTraits = 0; if (italic) { symbolicTraits |= UIFontDescriptorTraitItalic; } if (bold) { symbolicTraits |= UIFontDescriptorTraitBold; } UIFont *specialFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:symbolicTraits] size:font.pointSize]; return specialFont; } @end