1
This commit is contained in:
@@ -7,15 +7,18 @@
|
||||
|
||||
#import "KBAIReplyCell.h"
|
||||
#import "KBAIReplyModel.h"
|
||||
#import "KBTopImageButton.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
|
||||
@interface KBAIReplyCell ()
|
||||
|
||||
@property(nonatomic, strong) UIImageView *avatarImageView;
|
||||
@property(nonatomic, strong) UILabel *userNameLabel;
|
||||
@property(nonatomic, strong) UILabel *contentLabel;
|
||||
@property(nonatomic, strong) UILabel *timeLabel;
|
||||
@property(nonatomic, strong) UIButton *likeButton;
|
||||
@property(nonatomic, strong) UIButton *replyButton;
|
||||
@property(nonatomic, strong) KBTopImageButton *likeButton;
|
||||
|
||||
@end
|
||||
|
||||
@@ -36,36 +39,48 @@
|
||||
|
||||
- (void)setupUI {
|
||||
[self.contentView addSubview:self.avatarImageView];
|
||||
[self.contentView addSubview:self.userNameLabel];
|
||||
[self.contentView addSubview:self.contentLabel];
|
||||
[self.contentView addSubview:self.timeLabel];
|
||||
[self.contentView addSubview:self.replyButton];
|
||||
[self.contentView addSubview:self.likeButton];
|
||||
|
||||
// 左侧缩进 48pt(对齐一级评论内容)
|
||||
// 左侧缩进(对齐一级评论内容)
|
||||
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.contentView).offset(68); // 16 + 40 + 12 = 68
|
||||
make.top.equalTo(self.contentView).offset(8);
|
||||
make.width.height.mas_equalTo(28);
|
||||
}];
|
||||
|
||||
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
[self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.avatarImageView.mas_right).offset(8);
|
||||
make.top.equalTo(self.avatarImageView);
|
||||
make.right.equalTo(self.likeButton.mas_left).offset(-8);
|
||||
make.right.equalTo(self.contentView).offset(-50);
|
||||
}];
|
||||
|
||||
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.contentLabel);
|
||||
make.top.equalTo(self.contentLabel.mas_bottom).offset(4);
|
||||
// 降低优先级,避免与 TableView 设置的固定高度约束冲突
|
||||
make.left.equalTo(self.userNameLabel);
|
||||
make.top.equalTo(self.contentLabel.mas_bottom).offset(6);
|
||||
make.bottom.equalTo(self.contentView).offset(-8).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(8);
|
||||
make.width.mas_equalTo(40);
|
||||
make.width.mas_equalTo(30);
|
||||
make.height.mas_equalTo(30);
|
||||
}];
|
||||
|
||||
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.userNameLabel);
|
||||
make.top.equalTo(self.userNameLabel.mas_bottom).offset(4);
|
||||
make.right.equalTo(self.likeButton.mas_left).offset(-5);
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Configuration
|
||||
@@ -75,58 +90,54 @@
|
||||
sd_setImageWithURL:[NSURL URLWithString:reply.avatarUrl]
|
||||
placeholderImage:[UIImage imageNamed:@"default_avatar"]];
|
||||
|
||||
// 构建富文本:用户名(蓝色)+ @回复用户(蓝色)+ 内容
|
||||
NSMutableAttributedString *attrText =
|
||||
[[NSMutableAttributedString alloc] init];
|
||||
|
||||
// 用户名
|
||||
NSDictionary *nameAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:14
|
||||
weight:UIFontWeightMedium],
|
||||
NSForegroundColorAttributeName : [UIColor labelColor]
|
||||
};
|
||||
[attrText appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:reply.userName
|
||||
attributes:nameAttrs]];
|
||||
|
||||
// @回复用户
|
||||
// 用户名(如果有回复对象,显示 "用户名 回复 @被回复用户")
|
||||
if (reply.replyToUserName.length > 0) {
|
||||
NSMutableAttributedString *attrName = [[NSMutableAttributedString alloc] init];
|
||||
|
||||
NSDictionary *nameAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightMedium],
|
||||
NSForegroundColorAttributeName : [UIColor secondaryLabelColor]
|
||||
};
|
||||
[attrName appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:reply.userName
|
||||
attributes:nameAttrs]];
|
||||
|
||||
NSDictionary *replyAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:14],
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:13],
|
||||
NSForegroundColorAttributeName : [UIColor secondaryLabelColor]
|
||||
};
|
||||
[attrName appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:@" 回复 "
|
||||
attributes:replyAttrs]];
|
||||
|
||||
NSDictionary *toUserAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:13],
|
||||
NSForegroundColorAttributeName : [UIColor systemBlueColor]
|
||||
};
|
||||
NSString *replyTo =
|
||||
[NSString stringWithFormat:@" 回复 @%@", reply.replyToUserName];
|
||||
[attrText appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:replyTo
|
||||
attributes:replyAttrs]];
|
||||
[attrName appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:[NSString stringWithFormat:@"@%@", reply.replyToUserName]
|
||||
attributes:toUserAttrs]];
|
||||
|
||||
self.userNameLabel.attributedText = attrName;
|
||||
} else {
|
||||
self.userNameLabel.attributedText = nil;
|
||||
self.userNameLabel.text = reply.userName;
|
||||
}
|
||||
|
||||
// 内容
|
||||
NSDictionary *contentAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:14],
|
||||
NSForegroundColorAttributeName : [UIColor labelColor]
|
||||
};
|
||||
NSString *content = [NSString stringWithFormat:@":%@", reply.content];
|
||||
[attrText appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:content
|
||||
attributes:contentAttrs]];
|
||||
|
||||
self.contentLabel.attributedText = attrText;
|
||||
// 评论内容
|
||||
self.contentLabel.text = reply.content;
|
||||
|
||||
// 时间
|
||||
self.timeLabel.text = [reply formattedTime];
|
||||
|
||||
// 点赞
|
||||
NSString *likeText =
|
||||
reply.likeCount > 0 ? [self formatLikeCount:reply.likeCount] : @"";
|
||||
[self.likeButton setTitle:likeText forState:UIControlStateNormal];
|
||||
NSString *likeText = reply.likeCount > 0 ? [self formatLikeCount:reply.likeCount] : @"";
|
||||
self.likeButton.textLabel.text = likeText;
|
||||
|
||||
UIImage *likeImage = reply.isLiked ? [UIImage systemImageNamed:@"heart.fill"]
|
||||
: [UIImage systemImageNamed:@"heart"];
|
||||
[self.likeButton setImage:likeImage forState:UIControlStateNormal];
|
||||
self.likeButton.tintColor =
|
||||
reply.isLiked ? [UIColor systemRedColor] : [UIColor grayColor];
|
||||
self.likeButton.iconView.image = likeImage;
|
||||
self.likeButton.iconView.tintColor = reply.isLiked ? [UIColor systemRedColor] : [UIColor grayColor];
|
||||
}
|
||||
|
||||
- (NSString *)formatLikeCount:(NSInteger)count {
|
||||
@@ -146,6 +157,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)replyButtonTapped {
|
||||
if (self.onReplyAction) {
|
||||
self.onReplyAction();
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Lazy Loading
|
||||
|
||||
- (UIImageView *)avatarImageView {
|
||||
@@ -159,9 +176,21 @@
|
||||
return _avatarImageView;
|
||||
}
|
||||
|
||||
- (UILabel *)userNameLabel {
|
||||
if (!_userNameLabel) {
|
||||
_userNameLabel = [[UILabel alloc] init];
|
||||
_userNameLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
|
||||
_userNameLabel.textColor = [UIColor secondaryLabelColor];
|
||||
_userNameLabel.numberOfLines = 0;
|
||||
}
|
||||
return _userNameLabel;
|
||||
}
|
||||
|
||||
- (UILabel *)contentLabel {
|
||||
if (!_contentLabel) {
|
||||
_contentLabel = [[UILabel alloc] init];
|
||||
_contentLabel.font = [UIFont systemFontOfSize:14];
|
||||
_contentLabel.textColor = [UIColor labelColor];
|
||||
_contentLabel.numberOfLines = 0;
|
||||
}
|
||||
return _contentLabel;
|
||||
@@ -176,22 +205,31 @@
|
||||
return _timeLabel;
|
||||
}
|
||||
|
||||
- (UIButton *)likeButton {
|
||||
- (UIButton *)replyButton {
|
||||
if (!_replyButton) {
|
||||
_replyButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
_replyButton.titleLabel.font = [UIFont systemFontOfSize:11];
|
||||
[_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 = [UIButton buttonWithType:UIButtonTypeCustom];
|
||||
_likeButton.titleLabel.font = [UIFont systemFontOfSize:11];
|
||||
[_likeButton setTitleColor:[UIColor grayColor]
|
||||
forState:UIControlStateNormal];
|
||||
[_likeButton setImage:[UIImage systemImageNamed:@"heart"]
|
||||
forState:UIControlStateNormal];
|
||||
_likeButton.tintColor = [UIColor grayColor];
|
||||
_likeButton = [[KBTopImageButton alloc] init];
|
||||
_likeButton.iconSize = CGSizeMake(16, 16);
|
||||
_likeButton.spacing = 2;
|
||||
_likeButton.iconView.image = [UIImage systemImageNamed:@"heart"];
|
||||
_likeButton.iconView.tintColor = [UIColor grayColor];
|
||||
_likeButton.textLabel.font = [UIFont systemFontOfSize:10];
|
||||
_likeButton.textLabel.textColor = [UIColor grayColor];
|
||||
[_likeButton addTarget:self
|
||||
action:@selector(likeButtonTapped)
|
||||
forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
// 设置图片和文字间距
|
||||
_likeButton.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, 2);
|
||||
_likeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, -2);
|
||||
}
|
||||
return _likeButton;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user