Files
keyboard/keyBoard/Class/AiTalk/V/KBAIReplyCell.m

240 lines
7.9 KiB
Mathematica
Raw Normal View History

2026-01-16 15:55:08 +08:00
//
// KBAIReplyCell.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAIReplyCell.h"
#import "KBAIReplyModel.h"
2026-01-16 20:31:42 +08:00
#import "KBTopImageButton.h"
2026-01-16 15:55:08 +08:00
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
@interface KBAIReplyCell ()
@property(nonatomic, strong) UIImageView *avatarImageView;
2026-01-16 20:31:42 +08:00
@property(nonatomic, strong) UILabel *userNameLabel;
2026-01-16 15:55:08 +08:00
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UILabel *timeLabel;
2026-01-16 20:31:42 +08:00
@property(nonatomic, strong) UIButton *replyButton;
@property(nonatomic, strong) KBTopImageButton *likeButton;
2026-01-16 15:55:08 +08:00
@end
@implementation KBAIReplyCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
2026-01-28 13:43:36 +08:00
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
2026-01-16 15:55:08 +08:00
[self setupUI];
}
return self;
}
#pragma mark - UI Setup
- (void)setupUI {
[self.contentView addSubview:self.avatarImageView];
2026-01-16 20:31:42 +08:00
[self.contentView addSubview:self.userNameLabel];
2026-01-16 15:55:08 +08:00
[self.contentView addSubview:self.contentLabel];
[self.contentView addSubview:self.timeLabel];
2026-01-16 20:31:42 +08:00
[self.contentView addSubview:self.replyButton];
2026-01-16 15:55:08 +08:00
[self.contentView addSubview:self.likeButton];
2026-01-16 20:31:42 +08:00
//
2026-01-16 15:55:08 +08:00
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(68); // 16 + 40 + 12 = 68
make.top.equalTo(self.contentView).offset(8);
2026-01-28 20:18:18 +08:00
make.width.height.mas_equalTo(26);
2026-01-16 15:55:08 +08:00
}];
2026-01-16 20:31:42 +08:00
[self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
2026-01-16 15:55:08 +08:00
make.left.equalTo(self.avatarImageView.mas_right).offset(8);
make.top.equalTo(self.avatarImageView);
2026-01-16 20:31:42 +08:00
make.right.equalTo(self.contentView).offset(-50);
2026-01-16 15:55:08 +08:00
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
2026-01-16 20:31:42 +08:00
make.left.equalTo(self.userNameLabel);
make.top.equalTo(self.contentLabel.mas_bottom).offset(6);
2026-01-16 19:09:54 +08:00
make.bottom.equalTo(self.contentView).offset(-8).priority(MASLayoutPriorityDefaultHigh);
2026-01-16 15:55:08 +08:00
}];
2026-01-16 20:31:42 +08:00
[self.replyButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.timeLabel.mas_right).offset(16);
make.centerY.equalTo(self.timeLabel);
}];
2026-01-16 15:55:08 +08:00
[self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.contentView).offset(-16);
make.top.equalTo(self.contentView).offset(8);
2026-01-16 20:31:42 +08:00
make.width.mas_equalTo(30);
2026-01-16 15:55:08 +08:00
make.height.mas_equalTo(30);
}];
2026-01-16 20:31:42 +08:00
[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);
}];
2026-01-16 15:55:08 +08:00
}
#pragma mark - Configuration
- (void)configureWithReply:(KBAIReplyModel *)reply {
[self.avatarImageView
sd_setImageWithURL:[NSURL URLWithString:reply.avatarUrl]
placeholderImage:[UIImage imageNamed:@"default_avatar"]];
2026-01-16 20:31:42 +08:00
// "用户名 回复 @被回复用户"
2026-01-16 15:55:08 +08:00
if (reply.replyToUserName.length > 0) {
2026-01-16 20:31:42 +08:00
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]];
2026-01-16 15:55:08 +08:00
NSDictionary *replyAttrs = @{
2026-01-16 20:31:42 +08:00
NSFontAttributeName : [UIFont systemFontOfSize:13],
NSForegroundColorAttributeName : [UIColor secondaryLabelColor]
2026-01-16 15:55:08 +08:00
};
2026-01-16 20:31:42 +08:00
[attrName appendAttributedString:[[NSAttributedString alloc]
initWithString:@" 回复 "
2026-01-16 15:55:08 +08:00
attributes:replyAttrs]];
2026-01-16 20:31:42 +08:00
NSDictionary *toUserAttrs = @{
NSFontAttributeName : [UIFont systemFontOfSize:13],
NSForegroundColorAttributeName : [UIColor systemBlueColor]
};
[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;
2026-01-16 15:55:08 +08:00
}
2026-01-16 20:31:42 +08:00
//
self.contentLabel.text = reply.content;
2026-01-16 15:55:08 +08:00
//
self.timeLabel.text = [reply formattedTime];
//
2026-01-16 20:31:42 +08:00
NSString *likeText = reply.likeCount > 0 ? [self formatLikeCount:reply.likeCount] : @"";
self.likeButton.textLabel.text = likeText;
2026-01-16 15:55:08 +08:00
UIImage *likeImage = reply.isLiked ? [UIImage systemImageNamed:@"heart.fill"]
: [UIImage systemImageNamed:@"heart"];
2026-01-16 20:31:42 +08:00
self.likeButton.iconView.image = likeImage;
self.likeButton.iconView.tintColor = reply.isLiked ? [UIColor systemRedColor] : [UIColor grayColor];
2026-01-16 15:55:08 +08:00
}
- (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();
}
}
2026-01-16 20:31:42 +08:00
- (void)replyButtonTapped {
if (self.onReplyAction) {
self.onReplyAction();
}
}
2026-01-16 15:55:08 +08:00
#pragma mark - Lazy Loading
- (UIImageView *)avatarImageView {
if (!_avatarImageView) {
_avatarImageView = [[UIImageView alloc] init];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
2026-01-28 20:18:18 +08:00
_avatarImageView.layer.cornerRadius = 13;
2026-01-16 15:55:08 +08:00
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.backgroundColor = [UIColor systemGray5Color];
}
return _avatarImageView;
}
2026-01-16 20:31:42 +08:00
- (UILabel *)userNameLabel {
if (!_userNameLabel) {
_userNameLabel = [[UILabel alloc] init];
_userNameLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
2026-01-28 20:18:18 +08:00
_userNameLabel.textColor = [UIColor colorWithHex:0x9F9F9F];
2026-01-16 20:31:42 +08:00
_userNameLabel.numberOfLines = 0;
}
return _userNameLabel;
}
2026-01-16 15:55:08 +08:00
- (UILabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[UILabel alloc] init];
2026-01-28 20:18:18 +08:00
_contentLabel.font = [UIFont systemFontOfSize:12];
_contentLabel.textColor = [UIColor whiteColor];
2026-01-16 15:55:08 +08:00
_contentLabel.numberOfLines = 0;
}
return _contentLabel;
}
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] init];
2026-01-28 20:18:18 +08:00
_timeLabel.font = [UIFont systemFontOfSize:12];
_timeLabel.textColor = [UIColor colorWithHex:0x9F9F9F];
2026-01-16 15:55:08 +08:00
}
return _timeLabel;
}
2026-01-16 20:31:42 +08:00
- (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 {
2026-01-16 15:55:08 +08:00
if (!_likeButton) {
2026-01-16 20:31:42 +08:00
_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];
2026-01-16 15:55:08 +08:00
[_likeButton addTarget:self
action:@selector(likeButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
}
return _likeButton;
}
@end