This commit is contained in:
2026-01-16 20:31:42 +08:00
parent 663cb8493b
commit 93489b09d9
6 changed files with 302 additions and 116 deletions

View File

@@ -7,6 +7,7 @@
#import "KBAICommentHeaderView.h"
#import "KBAICommentModel.h"
#import "KBTopImageButton.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
@@ -14,9 +15,10 @@
@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;
@property(nonatomic, strong) UILabel *timeLabel;
@property(nonatomic, strong) UIButton *replyButton;
@property(nonatomic, strong) KBTopImageButton *likeButton;
@end
@@ -37,8 +39,9 @@
[self.contentView addSubview:self.avatarImageView];
[self.contentView addSubview:self.userNameLabel];
[self.contentView addSubview:self.timeLabel];
[self.contentView addSubview:self.contentLabel];
[self.contentView addSubview:self.timeLabel];
[self.contentView addSubview:self.replyButton];
[self.contentView addSubview:self.likeButton];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -53,24 +56,31 @@
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);
make.top.equalTo(self.contentLabel.mas_bottom).offset(8);
make.bottom.equalTo(self.contentView).offset(-12).priority(MASLayoutPriorityDefaultHigh);
}];
[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.replyButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.timeLabel.mas_right).offset(16);
make.centerY.equalTo(self.timeLabel);
}];
[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.width.mas_equalTo(30);
make.height.mas_equalTo(40);
}];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userNameLabel);
make.top.equalTo(self.userNameLabel.mas_bottom).offset(6);
make.right.equalTo(self.likeButton.mas_left).offset(-5);
}];
}
#pragma mark - Configuration
@@ -80,19 +90,19 @@
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;
self.timeLabel.text = [comment formattedTime];
//
NSString *likeText =
comment.likeCount > 0 ? [self formatLikeCount:comment.likeCount] : @"赞";
[self.likeButton setTitle:likeText forState:UIControlStateNormal];
self.likeButton.textLabel.text = likeText;
UIImage *likeImage = comment.isLiked
? [UIImage systemImageNamed:@"heart.fill"]
: [UIImage systemImageNamed:@"heart"];
[self.likeButton setImage:likeImage forState:UIControlStateNormal];
self.likeButton.tintColor =
self.likeButton.iconView.image = likeImage;
self.likeButton.iconView.tintColor =
comment.isLiked ? [UIColor systemRedColor] : [UIColor grayColor];
}
@@ -113,6 +123,12 @@
}
}
- (void)replyButtonTapped {
if (self.onReplyAction) {
self.onReplyAction();
}
}
#pragma mark - Lazy Loading
- (UIImageView *)avatarImageView {
@@ -129,22 +145,12 @@
- (UILabel *)userNameLabel {
if (!_userNameLabel) {
_userNameLabel = [[UILabel alloc] init];
_userNameLabel.font = [UIFont systemFontOfSize:14
weight:UIFontWeightMedium];
_userNameLabel.textColor = [UIColor labelColor];
_userNameLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
_userNameLabel.textColor = [UIColor secondaryLabelColor];
}
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];
@@ -155,19 +161,37 @@
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];
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] init];
_timeLabel.font = [UIFont systemFontOfSize:12];
_timeLabel.textColor = [UIColor secondaryLabelColor];
}
return _timeLabel;
}
//
_likeButton.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter;
- (UIButton *)replyButton {
if (!_replyButton) {
_replyButton = [UIButton buttonWithType:UIButtonTypeCustom];
_replyButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_replyButton setTitle:@"回复" forState:UIControlStateNormal];
[_replyButton setTitleColor:[UIColor secondaryLabelColor] forState:UIControlStateNormal];
[_replyButton addTarget:self
action:@selector(replyButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
}
return _replyButton;
}
- (KBTopImageButton *)likeButton {
if (!_likeButton) {
_likeButton = [[KBTopImageButton alloc] init];
_likeButton.iconSize = CGSizeMake(20, 20);
_likeButton.spacing = 2;
_likeButton.iconView.image = [UIImage systemImageNamed:@"heart"];
_likeButton.iconView.tintColor = [UIColor grayColor];
_likeButton.textLabel.font = [UIFont systemFontOfSize:11];
_likeButton.textLabel.textColor = [UIColor grayColor];
[_likeButton addTarget:self
action:@selector(likeButtonTapped)
forControlEvents:UIControlEventTouchUpInside];