// // KBAICommentHeaderView.m // keyBoard // // Created by Mac on 2026/1/16. // #import "KBAICommentHeaderView.h" #import "KBAICommentModel.h" #import #import @interface KBAICommentHeaderView () @property(nonatomic, strong) UIImageView *avatarImageView; @property(nonatomic, strong) UILabel *userNameLabel; @property(nonatomic, strong) UILabel *timeLabel; @property(nonatomic, strong) UILabel *contentLabel; @property(nonatomic, strong) UIButton *likeButton; @end @implementation KBAICommentHeaderView - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithReuseIdentifier:reuseIdentifier]; if (self) { [self setupUI]; } return self; } #pragma mark - UI Setup - (void)setupUI { self.contentView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.avatarImageView]; [self.contentView addSubview:self.userNameLabel]; [self.contentView addSubview:self.timeLabel]; [self.contentView addSubview:self.contentLabel]; [self.contentView addSubview:self.likeButton]; [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(16); make.top.equalTo(self.contentView).offset(12); make.width.height.mas_equalTo(40); }]; [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.avatarImageView.mas_right).offset(12); make.top.equalTo(self.avatarImageView); make.right.lessThanOrEqualTo(self.likeButton.mas_left).offset(-10); }]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.userNameLabel); make.top.equalTo(self.userNameLabel.mas_bottom).offset(2); }]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.userNameLabel); make.top.equalTo(self.timeLabel.mas_bottom).offset(8); make.right.equalTo(self.contentView).offset(-50); make.bottom.equalTo(self.contentView).offset(-12).priority(MASLayoutPriorityDefaultHigh); }]; [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.top.equalTo(self.contentView).offset(12); make.width.mas_equalTo(50); make.height.mas_equalTo(40); }]; } #pragma mark - Configuration - (void)configureWithComment:(KBAICommentModel *)comment { [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:comment.avatarUrl] placeholderImage:[UIImage imageNamed:@"default_avatar"]]; self.userNameLabel.text = comment.userName; self.timeLabel.text = [comment formattedTime]; self.contentLabel.text = comment.content; // 点赞按钮 NSString *likeText = comment.likeCount > 0 ? [self formatLikeCount:comment.likeCount] : @"赞"; [self.likeButton setTitle:likeText forState:UIControlStateNormal]; UIImage *likeImage = comment.isLiked ? [UIImage systemImageNamed:@"heart.fill"] : [UIImage systemImageNamed:@"heart"]; [self.likeButton setImage:likeImage forState:UIControlStateNormal]; self.likeButton.tintColor = comment.isLiked ? [UIColor systemRedColor] : [UIColor grayColor]; } - (NSString *)formatLikeCount:(NSInteger)count { if (count >= 10000) { return [NSString stringWithFormat:@"%.1fw", count / 10000.0]; } else if (count >= 1000) { return [NSString stringWithFormat:@"%.1fk", count / 1000.0]; } return [NSString stringWithFormat:@"%ld", (long)count]; } #pragma mark - Actions - (void)likeButtonTapped { if (self.onLikeAction) { self.onLikeAction(); } } #pragma mark - Lazy Loading - (UIImageView *)avatarImageView { if (!_avatarImageView) { _avatarImageView = [[UIImageView alloc] init]; _avatarImageView.contentMode = UIViewContentModeScaleAspectFill; _avatarImageView.layer.cornerRadius = 20; _avatarImageView.layer.masksToBounds = YES; _avatarImageView.backgroundColor = [UIColor systemGray5Color]; } return _avatarImageView; } - (UILabel *)userNameLabel { if (!_userNameLabel) { _userNameLabel = [[UILabel alloc] init]; _userNameLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _userNameLabel.textColor = [UIColor labelColor]; } return _userNameLabel; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] init]; _timeLabel.font = [UIFont systemFontOfSize:12]; _timeLabel.textColor = [UIColor secondaryLabelColor]; } return _timeLabel; } - (UILabel *)contentLabel { if (!_contentLabel) { _contentLabel = [[UILabel alloc] init]; _contentLabel.font = [UIFont systemFontOfSize:15]; _contentLabel.textColor = [UIColor labelColor]; _contentLabel.numberOfLines = 0; } return _contentLabel; } - (UIButton *)likeButton { if (!_likeButton) { _likeButton = [UIButton buttonWithType:UIButtonTypeCustom]; _likeButton.titleLabel.font = [UIFont systemFontOfSize:12]; [_likeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [_likeButton setImage:[UIImage systemImageNamed:@"heart"] forState:UIControlStateNormal]; _likeButton.tintColor = [UIColor grayColor]; // 图片在上,文字在下的布局 _likeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; [_likeButton addTarget:self action:@selector(likeButtonTapped) forControlEvents:UIControlEventTouchUpInside]; } return _likeButton; } @end