// // MOSystemNormalCell.m // TUIChat // // Created by SuperC on 2025/5/16. // #define UIColorFromHex(HexValue) [UIColor colorWithRed:((float)((HexValue & 0xFF0000) >> 16))/255.0 green:((float)((HexValue & 0xFF00) >> 8))/255.0 blue:((float)(HexValue & 0xFF))/255.0 alpha:1.0] #import "MOSystemNormalCell.h" @implementation MOSystemNormalCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ self.backgroundColor = [UIColor clearColor]; self.contentView.backgroundColor = [UIColor clearColor]; //Cell 去除选中效果 self.selectionStyle = UITableViewCellSelectionStyleNone; [self.contentView addSubview:self.bgView]; [self.bgView addSubview:self.iconImgView]; [self.bgView addSubview:self.groupLabel]; [self.bgView addSubview:self.titleLab]; [self.bgView addSubview:self.timeLab]; [self.bgView addSubview:self.pictureView]; [self.bgView addSubview:self.contentLab]; [self.bgView addSubview:self.detailsView]; [self setupView]; } return self; } - (void)setupView{ [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.contentView).offset(15.0); make.trailing.equalTo(self.contentView).offset(-15.0); make.top.equalTo(self.contentView).offset(7.5); make.bottom.equalTo(self.contentView).offset(-7.5); }]; [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.bgView).offset(10.0); make.top.equalTo(self.bgView).offset(8.0); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; [self.groupLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.mas_equalTo(40); make.top.mas_equalTo(13); make.width.lessThanOrEqualTo(@180); }]; [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(40); make.leading.mas_equalTo(10); make.trailing.mas_equalTo(-10); make.height.mas_equalTo(75); }]; [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.equalTo(self.bgView).offset(-10.0); make.top.mas_equalTo(13); make.width.lessThanOrEqualTo(@180); }]; [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(40); make.leading.mas_equalTo(10); make.trailing.mas_equalTo(-10); }]; [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.equalTo(self.bgView).offset(10.0); make.trailing.equalTo(self.bgView).offset(-10.0); make.bottom.mas_equalTo(-41); }]; [self.detailsView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.leading.trailing.mas_equalTo(0); make.height.mas_equalTo(30); }]; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (UIImage *)getAppIcon { NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary]; NSString *iconName = nil; // iOS 5+ 的 AppIcon 路径解析 NSDictionary *iconsDict = infoPlist[@"CFBundleIcons"]; NSDictionary *primaryIcons = iconsDict[@"CFBundlePrimaryIcon"]; NSArray *iconFiles = primaryIcons[@"CFBundleIconFiles"]; // 获取最后一个(最大尺寸)icon iconName = iconFiles.lastObject; return [UIImage imageNamed:iconName]; } - (void)fillWithData:(MOSystemNormalCellData *)data { [super fillWithData:data]; _dataModel = data; [self.iconImgView setImage:[self getAppIcon]]; // if([data.innerMessage.sender isEqualToString:@"10000"]){ // //官方消息 // [self.iconImgView setImage:[UIImage imageNamed:@"icon_im_official_news" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]]; // } // else{ // //官方活动 // [self.iconImgView setImage:[UIImage imageNamed:@"icon_im_activity_noti" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]]; // } self.contentLab.text = data.content; self.timeLab.text = [TUITool convertDateToDetailStr:data.innerMessage.timestamp]; if ([self.dataModel showPicture]) {//显示图片 self.pictureView.hidden = NO; [self.pictureView sd_setImageWithURL:[NSURL URLWithString:data.img]]; } else {//不显示图片 self.pictureView.hidden = YES; } if ([self.dataModel showTitle]) { self.titleLab.hidden = NO; self.titleLab.text = data.title; if (!self.pictureView.hidden) {//显示图片 [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(125); }]; } else {//不显示图片 [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(40); }]; } } else { self.titleLab.hidden = YES; } if ([self.dataModel showDetailsView]) {//显示底部detailsView self.detailsView.hidden = NO; [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-41); }]; } else {//不显示底部detailsView self.detailsView.hidden = YES; [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-10); }]; } if (data.linkStr.length > 0) {//富文本 NSString *fullText = data.content; NSRange linkRange = [fullText rangeOfString:data.linkStr]; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:fullText]; if (linkRange.location != NSNotFound) { [attrStr addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x0BDDFC) range:linkRange]; } self.contentLab.attributedText = attrStr; self.contentLab.userInteractionEnabled = YES; // 添加点击手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailViewClick)]; [self.contentLab addGestureRecognizer:tap]; } else { self.contentLab.text = data.content; } } - (void)detailViewClick { if (self.dataModel.link) { [[NSNotificationCenter defaultCenter] postNotificationName:@"MOChatJumpLink" object:self.dataModel.link]; } } - (void)layoutSubviews { [super layoutSubviews]; } #pragma mark - TUIMessageCellProtocol + (CGSize)getContentSize:(TUIMessageCellData *)data { MOSystemNormalCellData *evaluationCellData = (MOSystemNormalCellData *)data; CGFloat topTitleHeight = 15.0 / 2.0 + 8.0 + 24.0 + 8.0; CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 15.0 * 2.0; CGFloat contentWidth = viewWidth - 10.0 * 2.0; CGRect contentRect; if(evaluationCellData.type == 9){ contentRect = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil]; contentRect.size.height = contentRect.size.height + topTitleHeight + 10.0 + 15.0 / 2.0; } CGSize size = CGSizeMake(245, ceilf(contentRect.size.height)); return size; } #pragma mark - Lazy - (UIView *)bgView{ if (_bgView == nil){ _bgView = [UIView new]; _bgView.layer.cornerRadius = 12.0; _bgView.layer.masksToBounds = YES; _bgView.backgroundColor = [UIColor whiteColor]; } return _bgView; } - (UIImageView *)iconImgView{ if (!_iconImgView){ _iconImgView = [[UIImageView alloc] init]; _iconImgView.userInteractionEnabled = NO; _iconImgView.layer.cornerRadius = 6; _iconImgView.layer.masksToBounds = YES; } return _iconImgView; } - (UILabel *)groupLabel { if (!_groupLabel) { _groupLabel = [UILabel new]; _groupLabel.textColor = UIColorFromHex(0x666666); _groupLabel.textAlignment = NSTextAlignmentLeft; _groupLabel.font = [MOSystemNormalCell getTheFontWithSize:13.0 AndFontName:@"Roboto"]; _groupLabel.text = @"MiMo Team"; } return _groupLabel; } - (UILabel *)titleLab{ if(!_titleLab){ _titleLab = [UILabel new]; _titleLab.numberOfLines = 0; _titleLab.textColor = UIColorFromHex(0x666666); _titleLab.textAlignment = NSTextAlignmentLeft; _titleLab.font = [MOSystemNormalCell getTheFontWithSize:15.0 AndFontName:@"Roboto-Bold"]; } return _titleLab; } - (UILabel *)timeLab{ if(!_timeLab){ _timeLab = [UILabel new]; _timeLab.textColor = UIColorFromHex(0x333333); _timeLab.textAlignment = NSTextAlignmentRight; _timeLab.font = [MOSystemNormalCell getTheFontWithSize:12.0 AndFontName:@"Roboto"]; } return _timeLab; } - (UIImageView *)pictureView { if (!_pictureView) { _pictureView = [[UIImageView alloc] init]; _pictureView.contentMode = UIViewContentModeScaleAspectFill; _pictureView.layer.masksToBounds = YES; _pictureView.layer.cornerRadius = 12; } return _pictureView; } - (UILabel *)contentLab{ if(!_contentLab){ _contentLab = [UILabel new]; _contentLab.textColor = UIColorFromHex(0x333333); _contentLab.textAlignment = NSTextAlignmentLeft; _contentLab.font = [MOSystemNormalCell getTheFontWithSize:13.0 AndFontName:@"Roboto"]; _contentLab.numberOfLines = 0; } return _contentLab; } - (UIView *)detailsView { if (!_detailsView) { _detailsView = [[UIView alloc] init]; UIView *lineView = [[UIView alloc] init]; lineView.backgroundColor = UIColorFromHex(0xEEEEEE); [_detailsView addSubview:lineView]; [lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.mas_equalTo(10); make.right.mas_equalTo(-10); make.height.mas_equalTo(1); }]; UILabel *detailLabel = [[UILabel alloc] init]; detailLabel.textColor = UIColorFromHex(0x0BDDFC); detailLabel.font = [MOSystemNormalCell getTheFontWithSize:12.0 AndFontName:@"Roboto"]; detailLabel.text = @"View details"; [_detailsView addSubview:detailLabel]; [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(10); make.centerY.mas_equalTo(0); }]; UIImageView *arrowImageView = [[UIImageView alloc] init]; [arrowImageView setImage:[UIImage imageNamed:@"icon_im_arrow" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]]; [_detailsView addSubview:arrowImageView]; [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-10); make.centerY.mas_equalTo(0); make.size.mas_equalTo(CGSizeMake(12, 12)); }]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailViewClick)]; [_detailsView addGestureRecognizer:tap]; } return _detailsView; } #pragma mark - 类方法 + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{ UIFont *customFont = [UIFont fontWithName:fontName size:fontSize]; if(!customFont){ customFont = [MOSystemNormalCell 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