// // KBAIMessageCell.m // keyBoard // // Created by Mac on 2026/1/28. // #import "KBAIMessageCell.h" #import #import @interface KBAIMessageCell () @property (nonatomic, strong, readwrite) UIImageView *avatarImageView; @property (nonatomic, strong, readwrite) UILabel *nameLabel; @property (nonatomic, strong, readwrite) UILabel *contentLabel; @property (nonatomic, strong, readwrite) UILabel *timeLabel; @property (nonatomic, strong, readwrite) UIImageView *pinIconView; @end @implementation KBAIMessageCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor clearColor]; [self setupSubviews]; } return self; } - (void)setupSubviews { [self.contentView addSubview:self.avatarImageView]; [self.contentView addSubview:self.nameLabel]; [self.contentView addSubview:self.contentLabel]; [self.contentView addSubview:self.timeLabel]; [self.contentView addSubview:self.pinIconView]; [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(16); make.centerY.equalTo(self.contentView); make.width.height.mas_equalTo(50); }]; [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.avatarImageView.mas_right).offset(12); make.top.equalTo(self.avatarImageView).offset(2); make.right.lessThanOrEqualTo(self.timeLabel.mas_left).offset(-8); }]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.nameLabel); make.top.equalTo(self.nameLabel.mas_bottom).offset(4); make.right.lessThanOrEqualTo(self.timeLabel.mas_left).offset(-8); }]; [self.pinIconView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.top.equalTo(self.nameLabel); make.width.height.mas_equalTo(16); }]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.bottom.equalTo(self.contentLabel); }]; } - (void)configWithAvatar:(NSString *)avatarUrl name:(NSString *)name content:(NSString *)content time:(NSString *)time isPinned:(BOOL)isPinned { if (avatarUrl.length > 0) { [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:KBAvatarPlaceholderImage]; } else { self.avatarImageView.image = KBAvatarPlaceholderImage; } self.nameLabel.text = name; self.contentLabel.text = content; self.timeLabel.text = time; self.pinIconView.hidden = !isPinned; // 如果有置顶图标,时间标签需要调整位置 if (isPinned) { [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.bottom.equalTo(self.contentLabel); }]; } else { [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.bottom.equalTo(self.contentLabel); }]; } } #pragma mark - Lazy Load - (UIImageView *)avatarImageView { if (!_avatarImageView) { _avatarImageView = [[UIImageView alloc] init]; _avatarImageView.contentMode = UIViewContentModeScaleAspectFill; _avatarImageView.layer.cornerRadius = 25; _avatarImageView.layer.masksToBounds = YES; _avatarImageView.backgroundColor = [UIColor colorWithHex:0xF5F5F5]; } return _avatarImageView; } - (UILabel *)nameLabel { if (!_nameLabel) { _nameLabel = [[UILabel alloc] init]; _nameLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium]; _nameLabel.textColor = [UIColor colorWithHex:0x1B1F1A]; } return _nameLabel; } - (UILabel *)contentLabel { if (!_contentLabel) { _contentLabel = [[UILabel alloc] init]; _contentLabel.font = [UIFont systemFontOfSize:14]; _contentLabel.textColor = [UIColor colorWithHex:0x9F9F9F]; _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail; } return _contentLabel; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] init]; _timeLabel.font = [UIFont systemFontOfSize:12]; _timeLabel.textColor = [UIColor colorWithHex:0x9F9F9F]; _timeLabel.textAlignment = NSTextAlignmentRight; } return _timeLabel; } - (UIImageView *)pinIconView { if (!_pinIconView) { _pinIconView = [[UIImageView alloc] init]; _pinIconView.contentMode = UIViewContentModeScaleAspectFit; // 使用系统图标或自定义图标 if (@available(iOS 13.0, *)) { _pinIconView.image = [UIImage systemImageNamed:@"pin.fill"]; _pinIconView.tintColor = [UIColor colorWithHex:0x9F9F9F]; } _pinIconView.hidden = YES; } return _pinIconView; } @end