// // MOUserNotificationCellData.m // TUIChat // // Created by SuperC on 2025/10/13. // #import "MOUserNotificationCellData.h" @implementation MOUserNotificationCellData + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message { NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil]; if (param == nil) { return nil; } MOUserNotificationCellData *cellData = [[MOUserNotificationCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming]; cellData.reuseId = @"MOUserNotificationCell"; cellData.innerMessage = message; cellData.type = [[MOUserNotificationCellData objectOrNilForKey:@"type" fromDictionary:param] integerValue]; cellData.img = [MOUserNotificationCellData objectOrNilForKey:@"img" fromDictionary:param]; cellData.title = [MOUserNotificationCellData objectOrNilForKey:@"title" fromDictionary:param]; cellData.content = [MOUserNotificationCellData objectOrNilForKey:@"content" fromDictionary:param]; cellData.link = [MOUserNotificationCellData objectOrNilForKey:@"link" fromDictionary:param]; cellData.linkStr = [MOUserNotificationCellData 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 = [MOUserNotificationCellData objectOrNilForKey:@"content" fromDictionary:param]; if(contentStr.length > 0){ return contentStr; } return message.customElem.desc; } - (CGSize)contentSize{ //文字内容显示区域 CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0; CGFloat contentWidth = viewWidth - 10.0 * 2.0; //标题以上固定距离 CGFloat titleTopSpacing = 8.0; //标题高度 CGFloat titleHeight = 0; if (self.title.length > 0) { UIFont *titleFont = [MOUserNotificationCellData getTheFontWithSize:12.0 AndFontName:@"Inter-Regular_Medium"]; 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 = [MOUserNotificationCellData getTheFontWithSize:10.0 AndFontName:@"Inter-Regular"]; contentHeight = [self.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : contentFont} context:nil].size.height; } //30是bgView上下距离 CGFloat bgViewBottomSpacing = 8.0; CGFloat totalHeight = titleTopSpacing + titleHeight + 2.0 + contentHeight + bgViewBottomSpacing; CGSize size = CGSizeMake(245, ceilf(totalHeight)); return size; } #pragma mark - Tools + (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 = [MOUserNotificationCellData 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