| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- //
- // 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
|