添加评论
This commit is contained in:
198
keyBoard/Class/AiTalk/V/KBAIReplyCell.m
Normal file
198
keyBoard/Class/AiTalk/V/KBAIReplyCell.m
Normal file
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// KBAIReplyCell.m
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/16.
|
||||
//
|
||||
|
||||
#import "KBAIReplyCell.h"
|
||||
#import "KBAIReplyModel.h"
|
||||
#import <Masonry/Masonry.h>
|
||||
#import <SDWebImage/SDWebImage.h>
|
||||
|
||||
@interface KBAIReplyCell ()
|
||||
|
||||
@property(nonatomic, strong) UIImageView *avatarImageView;
|
||||
@property(nonatomic, strong) UILabel *contentLabel;
|
||||
@property(nonatomic, strong) UILabel *timeLabel;
|
||||
@property(nonatomic, strong) UIButton *likeButton;
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBAIReplyCell
|
||||
|
||||
- (instancetype)initWithStyle:(UITableViewCellStyle)style
|
||||
reuseIdentifier:(NSString *)reuseIdentifier {
|
||||
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
|
||||
if (self) {
|
||||
self.selectionStyle = UITableViewCellSelectionStyleNone;
|
||||
self.backgroundColor = [UIColor whiteColor];
|
||||
[self setupUI];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - UI Setup
|
||||
|
||||
- (void)setupUI {
|
||||
[self.contentView addSubview:self.avatarImageView];
|
||||
[self.contentView addSubview:self.contentLabel];
|
||||
[self.contentView addSubview:self.timeLabel];
|
||||
[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) {
|
||||
make.left.equalTo(self.avatarImageView.mas_right).offset(8);
|
||||
make.top.equalTo(self.avatarImageView);
|
||||
make.right.equalTo(self.likeButton.mas_left).offset(-8);
|
||||
}];
|
||||
|
||||
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.equalTo(self.contentLabel);
|
||||
make.top.equalTo(self.contentLabel.mas_bottom).offset(4);
|
||||
make.bottom.equalTo(self.contentView).offset(-8);
|
||||
}];
|
||||
|
||||
[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.height.mas_equalTo(30);
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Configuration
|
||||
|
||||
- (void)configureWithReply:(KBAIReplyModel *)reply {
|
||||
[self.avatarImageView
|
||||
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) {
|
||||
NSDictionary *replyAttrs = @{
|
||||
NSFontAttributeName : [UIFont systemFontOfSize:14],
|
||||
NSForegroundColorAttributeName : [UIColor systemBlueColor]
|
||||
};
|
||||
NSString *replyTo =
|
||||
[NSString stringWithFormat:@" 回复 @%@", reply.replyToUserName];
|
||||
[attrText appendAttributedString:[[NSAttributedString alloc]
|
||||
initWithString:replyTo
|
||||
attributes:replyAttrs]];
|
||||
}
|
||||
|
||||
// 内容
|
||||
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.timeLabel.text = [reply formattedTime];
|
||||
|
||||
// 点赞
|
||||
NSString *likeText =
|
||||
reply.likeCount > 0 ? [self formatLikeCount:reply.likeCount] : @"";
|
||||
[self.likeButton setTitle:likeText forState:UIControlStateNormal];
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
- (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 = 14;
|
||||
_avatarImageView.layer.masksToBounds = YES;
|
||||
_avatarImageView.backgroundColor = [UIColor systemGray5Color];
|
||||
}
|
||||
return _avatarImageView;
|
||||
}
|
||||
|
||||
- (UILabel *)contentLabel {
|
||||
if (!_contentLabel) {
|
||||
_contentLabel = [[UILabel alloc] init];
|
||||
_contentLabel.numberOfLines = 0;
|
||||
}
|
||||
return _contentLabel;
|
||||
}
|
||||
|
||||
- (UILabel *)timeLabel {
|
||||
if (!_timeLabel) {
|
||||
_timeLabel = [[UILabel alloc] init];
|
||||
_timeLabel.font = [UIFont systemFontOfSize:11];
|
||||
_timeLabel.textColor = [UIColor secondaryLabelColor];
|
||||
}
|
||||
return _timeLabel;
|
||||
}
|
||||
|
||||
- (UIButton *)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 addTarget:self
|
||||
action:@selector(likeButtonTapped)
|
||||
forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
// 设置图片和文字间距
|
||||
_likeButton.imageEdgeInsets = UIEdgeInsetsMake(0, -2, 0, 2);
|
||||
_likeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, -2);
|
||||
}
|
||||
return _likeButton;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user