// // MOUserLinkCell.m // TUIChat // // Created by SuperC on 2025/10/13. // #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 "MOUserLinkCell.h" @implementation MOUserLinkCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.bubbleView.hidden = YES; [self.container addSubview:self.bgView]; [self.container addSubview:self.titleLab]; [self.container addSubview:self.contentLab]; [self.container addSubview:self.lineView]; [self.container addSubview:self.detailTagLab]; [self.container addSubview:self.arrowImgView]; [self.container addSubview:self.actionBtn]; } return self; } - (void)fillWithData:(MOUserLinkCellData *)data{ [super fillWithData:data]; self.dataModel = data; self.titleLab.text = data.title; self.contentLab.text = data.content; // tell constraints they need updating [self setNeedsUpdateConstraints]; // update constraints now so we can animate the change [self updateConstraintsIfNeeded]; [self layoutIfNeeded]; } - (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 } + (BOOL)requiresConstraintBasedLayout { return YES; } #pragma mark - Lazy - (UIView *)bgView{ if(!_bgView){ _bgView = [[UIView alloc] init]; _bgView.backgroundColor = [UIColor whiteColor]; _bgView.layer.cornerRadius = 16.0; _bgView.layer.masksToBounds = YES; } return _bgView; } - (UILabel *)titleLab{ if(!_titleLab){ _titleLab = [UILabel new]; _titleLab.numberOfLines = 0; _titleLab.textColor = UIColorFromHex(0x000000); _titleLab.textAlignment = NSTextAlignmentLeft; _titleLab.font = [MOUserLinkCell getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"]; } return _titleLab; } - (UILabel *)contentLab{ if(!_contentLab){ _contentLab = [UILabel new]; _contentLab.textColor = UIColorFromHex(0x000000); _contentLab.textAlignment = NSTextAlignmentLeft; _contentLab.font = [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"]; _contentLab.numberOfLines = 0; } return _contentLab; } - (UIView *)lineView{ if(!_lineView){ _lineView = [[UIView alloc] init]; _lineView.backgroundColor = UIColorFromHex(0xF3F4FA); } return _lineView; } - (UILabel *)detailTagLab{ if(!_detailTagLab){ _detailTagLab = [UILabel new]; _detailTagLab.textColor = UIColorFromHex(0x4363FF); _detailTagLab.textAlignment = NSTextAlignmentLeft; _detailTagLab.font = [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"]; _detailTagLab.numberOfLines = 0; _detailTagLab.text = @"View details"; } return _detailTagLab; } - (UIImageView *)arrowImgView{ if(!_arrowImgView){ _arrowImgView = [[UIImageView alloc] init]; [_arrowImgView setImage:[UIImage imageNamed:@"icon_im_arrow" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]]; } return _arrowImgView; } - (UIButton *)actionBtn{ if(!_actionBtn){ _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_actionBtn setTitle:@"" forState:UIControlStateNormal]; [_actionBtn addTarget:self action:@selector(actionBtnClick) forControlEvents:UIControlEventTouchUpInside]; } return _actionBtn; } - (void)actionBtnClick{ if (self.dataModel.link) { [[NSNotificationCenter defaultCenter] postNotificationName:@"MOChatJumpLink" object:self.dataModel.link]; } } // this is Apple's recommended place for adding/updating constraints - (void)updateConstraints{ [super updateConstraints]; CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 64.0 - 12.0; CGFloat contentWidth = viewWidth - 12.0 * 2; CGRect titleRect = [self.titleLab.text boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [MOUserLinkCell getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"]} context:nil]; CGRect contentRect = [self.contentLab.text boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [MOUserLinkCell getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"]} context:nil]; [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.container); }]; [self.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(12); make.leading.mas_equalTo(12); make.width.mas_equalTo(245); make.height.mas_equalTo(titleRect.size.height); }]; [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.titleLab.mas_bottom).mas_offset(8); make.leading.mas_equalTo(12); make.width.mas_equalTo(245); make.height.mas_equalTo(contentRect.size.height); }]; [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.mas_equalTo(12); make.trailing.mas_equalTo(0); make.height.mas_equalTo(0.5); make.bottom.mas_equalTo(-40); }]; [self.detailTagLab mas_remakeConstraints:^(MASConstraintMaker *make) { make.leading.mas_equalTo(12); make.height.mas_equalTo(17); make.bottom.mas_equalTo(-12); }]; [self.arrowImgView mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.detailTagLab); make.trailing.mas_equalTo(-12); make.width.height.mas_equalTo(14); }]; [self.actionBtn mas_remakeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.container); }]; } - (void)layoutSubviews { [super layoutSubviews]; } #pragma mark - TUIMessageCellProtocol + (CGSize)getContentSize:(TUIMessageCellData *)data { MOUserLinkCellData *evaluationCellData = (MOUserLinkCellData *)data; CGFloat viewWidth = [[UIScreen mainScreen] bounds].size.width - 64.0 - 12.0; CGFloat contentWidth = viewWidth - 12.0 * 2; //标题以上固定距离 CGFloat titleTopSpacing = 0.0; //标题高度 CGFloat titleHeight = 0; if (evaluationCellData.title.length > 0) { UIFont *titleFont = [self getTheFontWithSize:16.0 AndFontName:@"Inter-Regular_Medium"]; titleHeight = [evaluationCellData.title boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : titleFont} context:nil].size.height + 2;//2是补偿高度 } //内容高度 CGFloat contentHeight = 0; if(evaluationCellData.content.length > 0){ UIFont *contentFont = [self getTheFontWithSize:14.0 AndFontName:@"Inter-Regular"]; contentHeight = [evaluationCellData.content boundingRectWithSize:CGSizeMake(contentWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : contentFont} context:nil].size.height; } //30是bgView上下距离 CGFloat bgViewBottomSpacing = 0.0; CGFloat totalHeight = titleTopSpacing + 12.0 + titleHeight + 8.0 + contentHeight + 10.0 + 40.0 + bgViewBottomSpacing; CGSize size = CGSizeMake(viewWidth, ceilf(totalHeight)); return size; } #pragma mark - 类方法 + (UIFont *)getTheFontWithSize:(CGFloat)fontSize AndFontName:(NSString *)fontName{ UIFont *customFont = [UIFont fontWithName:fontName size:fontSize]; if(!customFont){ customFont = [MOUserLinkCell 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