处理键盘

This commit is contained in:
2026-01-30 13:17:11 +08:00
parent 36135313d8
commit 36774a8a2c
13 changed files with 1447 additions and 65 deletions

View File

@@ -0,0 +1,40 @@
//
// KBChatAssistantCell.h
// CustomKeyboard
//
// AI 消息 Cell左侧显示带语音按钮和打字机效果
//
#import <UIKit/UIKit.h>
@class KBChatMessage;
@class KBChatAssistantCell;
NS_ASSUME_NONNULL_BEGIN
@protocol KBChatAssistantCellDelegate <NSObject>
@optional
/// 点击语音播放按钮
- (void)assistantCell:(KBChatAssistantCell *)cell didTapVoiceButtonForMessage:(KBChatMessage *)message;
@end
@interface KBChatAssistantCell : UITableViewCell
@property (nonatomic, weak) id<KBChatAssistantCellDelegate> delegate;
- (void)configureWithMessage:(KBChatMessage *)message;
/// 更新语音播放状态
- (void)updateVoicePlayingState:(BOOL)isPlaying;
/// 显示语音加载动画
- (void)showVoiceLoadingAnimation;
/// 隐藏语音加载动画
- (void)hideVoiceLoadingAnimation;
/// 停止打字机效果
- (void)stopTypewriterEffect;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,346 @@
//
// KBChatAssistantCell.m
// CustomKeyboard
//
// AI Cell
//
#import "KBChatAssistantCell.h"
#import "KBChatMessage.h"
#import "Masonry.h"
@interface KBChatAssistantCell ()
@property (nonatomic, strong) UIButton *voiceButton;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UIView *bubbleView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIActivityIndicatorView *voiceLoadingIndicator;
@property (nonatomic, strong) UIActivityIndicatorView *messageLoadingIndicator;
@property (nonatomic, strong) KBChatMessage *currentMessage;
///
@property (nonatomic, strong) NSTimer *typewriterTimer;
@property (nonatomic, copy) NSString *fullText;
@property (nonatomic, assign) NSInteger currentCharIndex;
@end
@implementation KBChatAssistantCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setupUI];
}
return self;
}
- (void)setupUI {
[self.contentView addSubview:self.voiceButton];
[self.contentView addSubview:self.durationLabel];
[self.contentView addSubview:self.voiceLoadingIndicator];
[self.contentView addSubview:self.messageLoadingIndicator];
[self.contentView addSubview:self.bubbleView];
[self.bubbleView addSubview:self.messageLabel];
//
[self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(12);
make.top.equalTo(self.contentView).offset(6);
make.width.height.mas_equalTo(20);
}];
//
[self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.voiceButton.mas_right).offset(4);
make.centerY.equalTo(self.voiceButton);
}];
//
[self.voiceLoadingIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.voiceButton);
}];
//
[self.messageLoadingIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(12);
make.top.equalTo(self.voiceButton.mas_bottom).offset(8);
}];
//
[self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.voiceButton.mas_bottom).offset(4);
make.bottom.equalTo(self.contentView).offset(-4);
make.left.equalTo(self.contentView).offset(12);
make.width.lessThanOrEqualTo(self.contentView).multipliedBy(0.7);
}];
//
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bubbleView).offset(8);
make.bottom.equalTo(self.bubbleView).offset(-8);
make.left.equalTo(self.bubbleView).offset(12);
make.right.equalTo(self.bubbleView).offset(-12);
make.height.greaterThanOrEqualTo(@18);
}];
}
- (void)configureWithMessage:(KBChatMessage *)message {
NSLog(@"[KBChatAssistantCell] ========== configureWithMessage ==========");
NSLog(@"[KBChatAssistantCell] text: %@", message.text);
NSLog(@"[KBChatAssistantCell] outgoing: %d, isLoading: %d, isComplete: %d, needsTypewriter: %d",
message.outgoing, message.isLoading, message.isComplete, message.needsTypewriterEffect);
//
[self stopTypewriterEffect];
self.currentMessage = message;
// loading
if (message.isLoading) {
NSLog(@"[KBChatAssistantCell] 显示 loading 状态");
self.messageLabel.attributedText = nil;
self.messageLabel.text = @"";
self.bubbleView.hidden = YES;
self.voiceButton.hidden = YES;
self.durationLabel.hidden = YES;
[self.messageLoadingIndicator startAnimating];
return;
}
// loading
[self.messageLoadingIndicator stopAnimating];
self.bubbleView.hidden = NO;
//
BOOL hasAudio = (message.audioId.length > 0) || (message.audioData.length > 0);
self.voiceButton.hidden = !hasAudio;
self.durationLabel.hidden = !hasAudio;
NSLog(@"[KBChatAssistantCell] hasAudio: %d, audioId: %@", hasAudio, message.audioId);
//
if (message.audioDuration > 0) {
NSInteger seconds = (NSInteger)ceil(message.audioDuration);
self.durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)seconds];
} else {
self.durationLabel.text = @"";
}
//
if (message.needsTypewriterEffect && !message.isComplete && message.text.length > 0) {
NSLog(@"[KBChatAssistantCell] ✅ 启动打字机效果");
[self startTypewriterEffectWithText:message.text];
} else {
NSLog(@"[KBChatAssistantCell] 直接显示文本(不使用打字机)");
self.messageLabel.attributedText = nil;
self.messageLabel.text = message.text ?: @"";
}
}
#pragma mark - Typewriter Effect
- (void)startTypewriterEffectWithText:(NSString *)text {
if (text.length == 0) return;
self.fullText = text;
self.currentCharIndex = 0;
//
self.messageLabel.text = text;
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
//
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
self.typewriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(typewriterTick)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.typewriterTimer forMode:NSRunLoopCommonModes];
[self typewriterTick];
});
}
- (void)typewriterTick {
NSString *text = self.fullText;
if (!text || text.length == 0) {
[self stopTypewriterEffect];
return;
}
if (self.currentCharIndex < text.length) {
self.currentCharIndex++;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
UIColor *textColor = [UIColor whiteColor];
if (self.currentCharIndex > 0) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:textColor
range:NSMakeRange(0, self.currentCharIndex)];
}
if (self.currentCharIndex < text.length) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(self.currentCharIndex, text.length - self.currentCharIndex)];
}
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
} else {
[self stopTypewriterEffect];
//
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor whiteColor]
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
//
if (self.currentMessage) {
self.currentMessage.isComplete = YES;
self.currentMessage.needsTypewriterEffect = NO;
}
}
}
- (void)stopTypewriterEffect {
if (self.typewriterTimer && self.typewriterTimer.isValid) {
[self.typewriterTimer invalidate];
}
self.typewriterTimer = nil;
self.currentCharIndex = 0;
self.fullText = nil;
}
#pragma mark - Voice Button
- (void)updateVoicePlayingState:(BOOL)isPlaying {
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = isPlaying ? [UIImage systemImageNamed:@"pause.circle.fill"] : [UIImage systemImageNamed:@"play.circle.fill"];
}
[self.voiceButton setImage:icon forState:UIControlStateNormal];
}
- (void)showVoiceLoadingAnimation {
[self.voiceButton setImage:nil forState:UIControlStateNormal];
[self.voiceLoadingIndicator startAnimating];
}
- (void)hideVoiceLoadingAnimation {
[self.voiceLoadingIndicator stopAnimating];
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = [UIImage systemImageNamed:@"play.circle.fill"];
}
[self.voiceButton setImage:icon forState:UIControlStateNormal];
}
- (void)voiceButtonTapped {
if ([self.delegate respondsToSelector:@selector(assistantCell:didTapVoiceButtonForMessage:)]) {
[self.delegate assistantCell:self didTapVoiceButtonForMessage:self.currentMessage];
}
}
#pragma mark - Reuse
- (void)prepareForReuse {
[super prepareForReuse];
[self stopTypewriterEffect];
self.messageLabel.text = @"";
self.messageLabel.attributedText = nil;
[self.messageLoadingIndicator stopAnimating];
[self.voiceLoadingIndicator stopAnimating];
}
- (void)dealloc {
[self stopTypewriterEffect];
}
#pragma mark - Lazy
- (UIButton *)voiceButton {
if (!_voiceButton) {
_voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = [UIImage systemImageNamed:@"play.circle.fill"];
}
[_voiceButton setImage:icon forState:UIControlStateNormal];
_voiceButton.tintColor = [UIColor whiteColor];
[_voiceButton addTarget:self action:@selector(voiceButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
return _voiceButton;
}
- (UILabel *)durationLabel {
if (!_durationLabel) {
_durationLabel = [[UILabel alloc] init];
_durationLabel.font = [UIFont systemFontOfSize:11];
_durationLabel.textColor = [UIColor whiteColor];
}
return _durationLabel;
}
- (UIActivityIndicatorView *)voiceLoadingIndicator {
if (!_voiceLoadingIndicator) {
_voiceLoadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
_voiceLoadingIndicator.color = [UIColor whiteColor];
_voiceLoadingIndicator.hidesWhenStopped = YES;
}
return _voiceLoadingIndicator;
}
- (UIActivityIndicatorView *)messageLoadingIndicator {
if (!_messageLoadingIndicator) {
_messageLoadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
_messageLoadingIndicator.color = [UIColor whiteColor];
_messageLoadingIndicator.hidesWhenStopped = YES;
}
return _messageLoadingIndicator;
}
- (UIView *)bubbleView {
if (!_bubbleView) {
_bubbleView = [[UIView alloc] init];
_bubbleView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.7];
_bubbleView.layer.cornerRadius = 12;
_bubbleView.layer.masksToBounds = YES;
}
return _bubbleView;
}
- (UILabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.numberOfLines = 0;
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.textColor = [UIColor whiteColor];
_messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
return _messageLabel;
}
@end

View File

@@ -5,13 +5,34 @@
#import <UIKit/UIKit.h>
@class KBChatMessage;
@class KBChatMessageCell;
NS_ASSUME_NONNULL_BEGIN
@protocol KBChatMessageCellDelegate <NSObject>
@optional
/// 点击语音播放按钮
- (void)chatMessageCell:(KBChatMessageCell *)cell didTapVoiceButtonForMessage:(KBChatMessage *)message;
@end
@interface KBChatMessageCell : UITableViewCell
@property (nonatomic, weak) id<KBChatMessageCellDelegate> delegate;
- (void)kb_configureWithMessage:(KBChatMessage *)message;
/// 更新语音播放状态
- (void)kb_updateVoicePlayingState:(BOOL)isPlaying;
/// 显示语音加载动画
- (void)kb_showVoiceLoadingAnimation;
/// 隐藏语音加载动画
- (void)kb_hideVoiceLoadingAnimation;
/// 停止打字机效果
- (void)kb_stopTypewriterEffect;
@end
NS_ASSUME_NONNULL_END

View File

@@ -8,12 +8,31 @@
#import "Masonry.h"
@interface KBChatMessageCell ()
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView *bubbleView;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIImageView *audioIconView;
@property (nonatomic, strong) UILabel *audioLabel;
///
@property (nonatomic, strong) UIButton *voiceButton;
///
@property (nonatomic, strong) UILabel *durationLabel;
///
@property (nonatomic, strong) UIActivityIndicatorView *voiceLoadingIndicator;
/// AI loading
@property (nonatomic, strong) UIActivityIndicatorView *messageLoadingIndicator;
///
@property (nonatomic, strong) KBChatMessage *currentMessage;
///
@property (nonatomic, strong) NSTimer *typewriterTimer;
@property (nonatomic, copy) NSString *fullText;
@property (nonatomic, assign) NSInteger currentCharIndex;
@end
@implementation KBChatMessageCell
@@ -26,6 +45,10 @@
[self.contentView addSubview:self.avatarView];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.voiceButton];
[self.contentView addSubview:self.durationLabel];
[self.contentView addSubview:self.voiceLoadingIndicator];
[self.contentView addSubview:self.messageLoadingIndicator];
[self.contentView addSubview:self.bubbleView];
[self.bubbleView addSubview:self.messageLabel];
[self.bubbleView addSubview:self.audioIconView];
@@ -35,6 +58,11 @@
}
- (void)kb_configureWithMessage:(KBChatMessage *)message {
//
[self kb_stopTypewriterEffect];
self.currentMessage = message;
BOOL outgoing = message.outgoing;
BOOL audioMessage = (!outgoing && message.audioFilePath.length > 0);
UIColor *bubbleColor = outgoing ? [UIColor colorWithHex:0x02BEAC] : [UIColor colorWithWhite:1 alpha:0.95];
@@ -50,7 +78,6 @@
self.messageLabel.textColor = textColor;
self.audioLabel.textColor = textColor;
self.audioIconView.tintColor = textColor;
self.messageLabel.text = message.text ?: @"";
self.audioLabel.text =
(message.text.length > 0) ? message.text : KBLocalized(@"语音回复");
self.messageLabel.hidden = audioMessage;
@@ -69,6 +96,43 @@
self.nameLabel.text =
(message.displayName.length > 0) ? message.displayName : KBLocalized(@"AI助手");
// loading
if (message.isLoading && !outgoing) {
self.bubbleView.hidden = YES;
self.voiceButton.hidden = YES;
self.durationLabel.hidden = YES;
[self.messageLoadingIndicator startAnimating];
[self kb_layoutForOutgoing:outgoing audioMessage:NO];
return;
}
// loading
[self.messageLoadingIndicator stopAnimating];
self.bubbleView.hidden = NO;
// AI audioId audioData
BOOL hasAudio = (!outgoing) && (message.audioId.length > 0 || message.audioData.length > 0);
self.voiceButton.hidden = !hasAudio;
self.durationLabel.hidden = !hasAudio;
if (hasAudio && message.audioDuration > 0) {
NSInteger seconds = (NSInteger)ceil(message.audioDuration);
self.durationLabel.text = [NSString stringWithFormat:@"%ld\"", (long)seconds];
} else {
self.durationLabel.text = @"";
}
//
if (!outgoing && message.needsTypewriterEffect && !message.isComplete && message.text.length > 0) {
[self kb_startTypewriterEffectWithText:message.text];
} else {
self.messageLabel.attributedText = nil;
self.messageLabel.text = message.text ?: @"";
}
[self kb_layoutForOutgoing:outgoing audioMessage:audioMessage];
}
- (void)kb_layoutForOutgoing:(BOOL)outgoing audioMessage:(BOOL)audioMessage {
CGFloat avatarSize = 28.0;
[self.avatarView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(avatarSize);
@@ -85,13 +149,45 @@
make.top.equalTo(self.contentView.mas_top).offset(0);
make.left.equalTo(self.contentView.mas_left);
}];
//
[self.voiceButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(0);
make.left.top.equalTo(self.contentView);
}];
[self.durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(0);
make.left.top.equalTo(self.contentView);
}];
} else {
[self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.avatarView.mas_right).offset(6);
make.top.equalTo(self.contentView.mas_top).offset(2);
make.right.lessThanOrEqualTo(self.contentView.mas_right).offset(-12);
}];
// AI
[self.voiceButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.avatarView.mas_right).offset(6);
make.top.equalTo(self.nameLabel.mas_bottom).offset(4);
make.width.height.mas_equalTo(20);
}];
[self.durationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.voiceButton.mas_right).offset(4);
make.centerY.equalTo(self.voiceButton);
}];
[self.voiceLoadingIndicator mas_remakeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.voiceButton);
}];
}
//
[self.messageLoadingIndicator mas_remakeConstraints:^(MASConstraintMaker *make) {
if (outgoing) {
make.right.equalTo(self.avatarView.mas_left).offset(-10);
} else {
make.left.equalTo(self.avatarView.mas_right).offset(10);
}
make.top.equalTo(self.nameLabel.mas_bottom).offset(8);
}];
[self.bubbleView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.lessThanOrEqualTo(self.contentView.mas_width).multipliedBy(0.65);
@@ -100,7 +196,8 @@
make.bottom.equalTo(self.contentView.mas_bottom).offset(-6);
make.right.equalTo(self.avatarView.mas_left).offset(-6);
} else {
make.top.equalTo(self.nameLabel.mas_bottom).offset(2);
// AI
make.top.equalTo(self.voiceButton.mas_bottom).offset(4);
make.bottom.equalTo(self.contentView.mas_bottom).offset(-6);
make.left.equalTo(self.avatarView.mas_right).offset(6);
make.right.lessThanOrEqualTo(self.contentView.mas_right).offset(-12);
@@ -142,6 +239,144 @@
}
}
#pragma mark - Typewriter Effect
- (void)kb_startTypewriterEffectWithText:(NSString *)text {
if (text.length == 0) return;
self.fullText = text;
self.currentCharIndex = 0;
//
self.messageLabel.text = text;
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
//
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
self.typewriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:@selector(kb_typewriterTick)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.typewriterTimer forMode:NSRunLoopCommonModes];
[self kb_typewriterTick];
});
}
- (void)kb_typewriterTick {
NSString *text = self.fullText;
if (!text || text.length == 0) {
[self kb_stopTypewriterEffect];
return;
}
if (self.currentCharIndex < text.length) {
self.currentCharIndex++;
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
UIColor *textColor = self.messageLabel.textColor ?: [UIColor blackColor];
if (self.currentCharIndex > 0) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:textColor
range:NSMakeRange(0, self.currentCharIndex)];
}
if (self.currentCharIndex < text.length) {
[attributedText addAttribute:NSForegroundColorAttributeName
value:[UIColor clearColor]
range:NSMakeRange(self.currentCharIndex, text.length - self.currentCharIndex)];
}
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
} else {
[self kb_stopTypewriterEffect];
//
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
UIColor *textColor = self.messageLabel.textColor ?: [UIColor blackColor];
[attributedText addAttribute:NSForegroundColorAttributeName
value:textColor
range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName
value:self.messageLabel.font
range:NSMakeRange(0, text.length)];
self.messageLabel.attributedText = attributedText;
//
if (self.currentMessage) {
self.currentMessage.isComplete = YES;
self.currentMessage.needsTypewriterEffect = NO;
}
}
}
- (void)kb_stopTypewriterEffect {
if (self.typewriterTimer && self.typewriterTimer.isValid) {
[self.typewriterTimer invalidate];
}
self.typewriterTimer = nil;
self.currentCharIndex = 0;
self.fullText = nil;
}
#pragma mark - Voice Button
- (void)kb_updateVoicePlayingState:(BOOL)isPlaying {
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = isPlaying ? [UIImage systemImageNamed:@"pause.circle.fill"] : [UIImage systemImageNamed:@"play.circle.fill"];
}
[self.voiceButton setImage:icon forState:UIControlStateNormal];
}
- (void)kb_showVoiceLoadingAnimation {
[self.voiceButton setImage:nil forState:UIControlStateNormal];
[self.voiceLoadingIndicator startAnimating];
}
- (void)kb_hideVoiceLoadingAnimation {
[self.voiceLoadingIndicator stopAnimating];
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = [UIImage systemImageNamed:@"play.circle.fill"];
}
[self.voiceButton setImage:icon forState:UIControlStateNormal];
}
- (void)kb_onVoiceButtonTapped {
if ([self.delegate respondsToSelector:@selector(chatMessageCell:didTapVoiceButtonForMessage:)]) {
[self.delegate chatMessageCell:self didTapVoiceButtonForMessage:self.currentMessage];
}
}
#pragma mark - Reuse
- (void)prepareForReuse {
[super prepareForReuse];
[self kb_stopTypewriterEffect];
self.messageLabel.text = @"";
self.messageLabel.attributedText = nil;
[self.messageLoadingIndicator stopAnimating];
[self.voiceLoadingIndicator stopAnimating];
}
- (void)dealloc {
[self kb_stopTypewriterEffect];
}
#pragma mark - Lazy
- (UIImageView *)avatarView {
@@ -209,6 +444,47 @@
return _audioLabel;
}
- (UIButton *)voiceButton {
if (!_voiceButton) {
_voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *icon = nil;
if (@available(iOS 13.0, *)) {
icon = [UIImage systemImageNamed:@"play.circle.fill"];
}
[_voiceButton setImage:icon forState:UIControlStateNormal];
_voiceButton.tintColor = [UIColor whiteColor];
[_voiceButton addTarget:self action:@selector(kb_onVoiceButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
return _voiceButton;
}
- (UILabel *)durationLabel {
if (!_durationLabel) {
_durationLabel = [[UILabel alloc] init];
_durationLabel.font = [UIFont systemFontOfSize:11];
_durationLabel.textColor = [UIColor whiteColor];
}
return _durationLabel;
}
- (UIActivityIndicatorView *)voiceLoadingIndicator {
if (!_voiceLoadingIndicator) {
_voiceLoadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
_voiceLoadingIndicator.color = [UIColor whiteColor];
_voiceLoadingIndicator.hidesWhenStopped = YES;
}
return _voiceLoadingIndicator;
}
- (UIActivityIndicatorView *)messageLoadingIndicator {
if (!_messageLoadingIndicator) {
_messageLoadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
_messageLoadingIndicator.color = [UIColor whiteColor];
_messageLoadingIndicator.hidesWhenStopped = YES;
}
return _messageLoadingIndicator;
}
- (UIImage *)kb_defaultAvatarImage {
if (@available(iOS 13.0, *)) {
return [UIImage systemImageNamed:@"person.circle.fill"];

View File

@@ -13,6 +13,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)chatPanelView:(KBChatPanelView *)view didSendText:(NSString *)text;
- (void)chatPanelView:(KBChatPanelView *)view didTapMessage:(KBChatMessage *)message;
- (void)chatPanelViewDidTapClose:(KBChatPanelView *)view;
/// 点击语音播放按钮
- (void)chatPanelView:(KBChatPanelView *)view didTapVoiceButtonForMessage:(KBChatMessage *)message;
@end
@interface KBChatPanelView : UIView
@@ -24,6 +26,24 @@ NS_ASSUME_NONNULL_BEGIN
//- (void)kb_setBackgroundImage:(nullable UIImage *)image;
- (void)kb_reloadWithMessages:(NSArray<KBChatMessage *> *)messages;
/// 添加用户消息
- (void)kb_addUserMessage:(NSString *)text;
/// 添加 loading 状态的 AI 消息
- (void)kb_addLoadingAssistantMessage;
/// 移除 loading 状态的 AI 消息
- (void)kb_removeLoadingAssistantMessage;
/// 添加 AI 消息(带打字机效果)
- (void)kb_addAssistantMessage:(NSString *)text audioId:(nullable NSString *)audioId;
/// 更新最后一条 AI 消息的音频数据
- (void)kb_updateLastAssistantMessageWithAudioData:(NSData *)audioData duration:(NSTimeInterval)duration;
/// 滚动到底部
- (void)kb_scrollToBottom;
@end
NS_ASSUME_NONNULL_END

View File

@@ -5,32 +5,33 @@
#import "KBChatPanelView.h"
#import "KBChatMessage.h"
#import "KBChatMessageCell.h"
#import "KBChatUserCell.h"
#import "KBChatAssistantCell.h"
#import "Masonry.h"
@interface KBChatPanelView () <UITableViewDataSource, UITableViewDelegate>
//@property (nonatomic, strong) UIImageView *backgroundImageView;
static NSString * const kUserCellIdentifier = @"KBChatUserCell";
static NSString * const kAssistantCellIdentifier = @"KBChatAssistantCell";
static const NSUInteger kKBChatMessageLimit = 10;
@interface KBChatPanelView () <UITableViewDataSource, UITableViewDelegate, KBChatAssistantCellDelegate>
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UITableView *tableViewInternal;
@property (nonatomic, copy) NSArray<KBChatMessage *> *messages;
@property (nonatomic, strong) NSMutableArray<KBChatMessage *> *messages;
@end
@implementation KBChatPanelView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSLog(@"[KBChatPanelView] ⚠️ initWithFrame 被调用self=%p", self);
self.backgroundColor = [UIColor clearColor];
self.messages = [NSMutableArray array];
// [self addSubview:self.backgroundImageView];
[self addSubview:self.headerView];
[self addSubview:self.tableViewInternal];
// [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self);
// }];
[self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.mas_top);
@@ -49,19 +50,167 @@
#pragma mark - Public
- (void)kb_reloadWithMessages:(NSArray<KBChatMessage *> *)messages {
self.messages = messages ?: @[];
NSLog(@"[KBChatPanelView] ========== kb_reloadWithMessages ==========");
NSLog(@"[KBChatPanelView] self=%p, 传入消息数量: %lu", self, (unsigned long)messages.count);
NSLog(@"[KBChatPanelView] 调用堆栈: %@", [NSThread callStackSymbols]);
[self.messages removeAllObjects];
if (messages.count > 0) {
[self.messages addObjectsFromArray:messages];
}
[self.tableViewInternal reloadData];
if (self.messages.count > 0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
[self.tableViewInternal scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
[self kb_scrollToBottom];
}
- (void)kb_addUserMessage:(NSString *)text {
if (text.length == 0) return;
NSLog(@"[KBChatPanelView] ========== kb_addUserMessage ==========");
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
NSLog(@"[KBChatPanelView] 添加用户消息: %@", text);
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
KBChatMessage *msg = [KBChatMessage userMessageWithText:text];
NSLog(@"[KBChatPanelView] 创建消息 - outgoing: %d, text: %@", msg.outgoing, msg.text);
[self kb_appendMessage:msg];
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
for (NSInteger i = 0; i < self.messages.count; i++) {
KBChatMessage *m = self.messages[i];
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
}
}
//- (void)kb_setBackgroundImage:(UIImage *)image {
// self.backgroundImageView.image = image;
//}
- (void)kb_addLoadingAssistantMessage {
NSLog(@"[KBChatPanelView] ========== kb_addLoadingAssistantMessage ==========");
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
KBChatMessage *msg = [KBChatMessage loadingAssistantMessage];
NSLog(@"[KBChatPanelView] 创建 loading 消息 - outgoing: %d, isLoading: %d", msg.outgoing, msg.isLoading);
[self kb_appendMessage:msg];
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
for (NSInteger i = 0; i < self.messages.count; i++) {
KBChatMessage *m = self.messages[i];
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
}
}
- (void)kb_removeLoadingAssistantMessage {
NSLog(@"[KBChatPanelView] ========== kb_removeLoadingAssistantMessage ==========");
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
for (NSInteger i = 0; i < self.messages.count; i++) {
KBChatMessage *m = self.messages[i];
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
}
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
KBChatMessage *msg = self.messages[i];
NSLog(@"[KBChatPanelView] 检查消息[%ld]: outgoing=%d, isLoading=%d", (long)i, msg.outgoing, msg.isLoading);
// AI outgoing == NO loading
if (!msg.outgoing && msg.isLoading) {
NSLog(@"[KBChatPanelView] ✅ 找到 loading AI 消息,准备移除索引: %ld", (long)i);
[self.messages removeObjectAtIndex:i];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableViewInternal deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"[KBChatPanelView] 移除后消息数量: %lu", (unsigned long)self.messages.count);
break;
}
}
NSLog(@"[KBChatPanelView] 最终消息数量: %lu", (unsigned long)self.messages.count);
for (NSInteger i = 0; i < self.messages.count; i++) {
KBChatMessage *m = self.messages[i];
NSLog(@"[KBChatPanelView] 最终消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
}
}
- (void)kb_addAssistantMessage:(NSString *)text audioId:(NSString *)audioId {
NSLog(@"[KBChatPanelView] ========== kb_addAssistantMessage ==========");
NSLog(@"[KBChatPanelView] self=%p, messages=%p", self, self.messages);
NSLog(@"[KBChatPanelView] AI 回复文本: %@", text);
NSLog(@"[KBChatPanelView] audioId: %@", audioId);
NSLog(@"[KBChatPanelView] 当前消息数量: %lu", (unsigned long)self.messages.count);
// loading
[self kb_removeLoadingAssistantMessage];
NSLog(@"[KBChatPanelView] 移除 loading 后消息数量: %lu", (unsigned long)self.messages.count);
KBChatMessage *msg = [KBChatMessage assistantMessageWithText:text audioId:audioId];
msg.displayName = KBLocalized(@"AI助手");
NSLog(@"[KBChatPanelView] 创建 AI 消息 - outgoing: %d, isLoading: %d, needsTypewriter: %d, text: %@",
msg.outgoing, msg.isLoading, msg.needsTypewriterEffect, msg.text);
[self kb_appendMessage:msg];
NSLog(@"[KBChatPanelView] 添加后消息数量: %lu", (unsigned long)self.messages.count);
for (NSInteger i = 0; i < self.messages.count; i++) {
KBChatMessage *m = self.messages[i];
NSLog(@"[KBChatPanelView] 消息[%ld]: outgoing=%d, isLoading=%d, text=%@", (long)i, m.outgoing, m.isLoading, m.text);
}
}
- (void)kb_updateLastAssistantMessageWithAudioData:(NSData *)audioData duration:(NSTimeInterval)duration {
for (NSInteger i = self.messages.count - 1; i >= 0; i--) {
KBChatMessage *msg = self.messages[i];
// AI outgoing == NO loading
if (!msg.outgoing && !msg.isLoading) {
msg.audioData = audioData;
msg.audioDuration = duration;
//
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
KBChatAssistantCell *cell = [self.tableViewInternal cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[KBChatAssistantCell class]]) {
// Cell
if (duration > 0) {
//
//
msg.needsTypewriterEffect = NO;
msg.isComplete = YES;
}
}
NSLog(@"[KBChatPanelView] 更新 AI 消息音频数据,时长: %.2f秒", duration);
break;
}
}
}
- (void)kb_scrollToBottom {
if (self.messages.count == 0) return;
[self.tableViewInternal layoutIfNeeded];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0];
[self.tableViewInternal scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
#pragma mark - Private
- (void)kb_appendMessage:(KBChatMessage *)message {
if (!message) return;
NSInteger oldCount = self.messages.count;
[self.messages addObject:message];
//
if (self.messages.count > kKBChatMessageLimit) {
NSUInteger overflow = self.messages.count - kKBChatMessageLimit;
[self.messages removeObjectsInRange:NSMakeRange(0, overflow)];
[self.tableViewInternal reloadData];
} else {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:oldCount inSection:0];
[self.tableViewInternal insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self kb_scrollToBottom];
});
}
#pragma mark - Actions
@@ -78,10 +227,31 @@
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
KBChatMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(KBChatMessageCell.class)];
NSLog(@"[KBChatPanelView] ========== cellForRowAtIndexPath: %ld ==========", (long)indexPath.row);
if (indexPath.row >= self.messages.count) {
NSLog(@"[KBChatPanelView] ❌ 索引越界,返回空 Cell");
return [[UITableViewCell alloc] init];
}
KBChatMessage *msg = self.messages[indexPath.row];
[cell kb_configureWithMessage:msg];
return cell;
NSLog(@"[KBChatPanelView] 消息: outgoing=%d, isLoading=%d, needsTypewriter=%d, text=%@",
msg.outgoing, msg.isLoading, msg.needsTypewriterEffect, msg.text);
if (msg.outgoing) {
//
NSLog(@"[KBChatPanelView] 使用 KBChatUserCell");
KBChatUserCell *cell = [tableView dequeueReusableCellWithIdentifier:kUserCellIdentifier forIndexPath:indexPath];
[cell configureWithMessage:msg];
return cell;
} else {
// AI
NSLog(@"[KBChatPanelView] 使用 KBChatAssistantCell");
KBChatAssistantCell *cell = [tableView dequeueReusableCellWithIdentifier:kAssistantCellIdentifier forIndexPath:indexPath];
cell.delegate = self;
[cell configureWithMessage:msg];
return cell;
}
}
#pragma mark - UITableViewDelegate
@@ -91,7 +261,7 @@
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
return 60.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
@@ -102,6 +272,14 @@
}
}
#pragma mark - KBChatAssistantCellDelegate
- (void)assistantCell:(KBChatAssistantCell *)cell didTapVoiceButtonForMessage:(KBChatMessage *)message {
if ([self.delegate respondsToSelector:@selector(chatPanelView:didTapVoiceButtonForMessage:)]) {
[self.delegate chatPanelView:self didTapVoiceButtonForMessage:message];
}
}
#pragma mark - Lazy
- (UITableView *)tableViewInternal {
@@ -112,9 +290,14 @@
_tableViewInternal.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableViewInternal.dataSource = self;
_tableViewInternal.delegate = self;
_tableViewInternal.estimatedRowHeight = 44.0;
_tableViewInternal.estimatedRowHeight = 60.0;
_tableViewInternal.rowHeight = UITableViewAutomaticDimension;
[_tableViewInternal registerClass:KBChatMessageCell.class forCellReuseIdentifier:NSStringFromClass(KBChatMessageCell.class)];
// Cell
[_tableViewInternal registerClass:KBChatUserCell.class forCellReuseIdentifier:kUserCellIdentifier];
[_tableViewInternal registerClass:KBChatAssistantCell.class forCellReuseIdentifier:kAssistantCellIdentifier];
if (@available(iOS 11.0, *)) {
_tableViewInternal.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}
return _tableViewInternal;
}
@@ -165,17 +348,6 @@
return _closeButton;
}
//- (UIImageView *)backgroundImageView {
// if (!_backgroundImageView) {
// _backgroundImageView = [[UIImageView alloc] init];
// _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
// _backgroundImageView.clipsToBounds = YES;
// _backgroundImageView.backgroundColor = [UIColor clearColor];
// _backgroundImageView.userInteractionEnabled = NO;
// }
// return _backgroundImageView;
//}
#pragma mark - Expose
- (UITableView *)tableView { return self.tableViewInternal; }

View File

@@ -0,0 +1,19 @@
//
// KBChatUserCell.h
// CustomKeyboard
//
// 用户消息 Cell右侧显示
//
#import <UIKit/UIKit.h>
@class KBChatMessage;
NS_ASSUME_NONNULL_BEGIN
@interface KBChatUserCell : UITableViewCell
- (void)configureWithMessage:(KBChatMessage *)message;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,85 @@
//
// KBChatUserCell.m
// CustomKeyboard
//
// Cell
//
#import "KBChatUserCell.h"
#import "KBChatMessage.h"
#import "Masonry.h"
@interface KBChatUserCell ()
@property (nonatomic, strong) UIView *bubbleView;
@property (nonatomic, strong) UILabel *messageLabel;
@end
@implementation KBChatUserCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setupUI];
}
return self;
}
- (void)setupUI {
[self.contentView addSubview:self.bubbleView];
[self.bubbleView addSubview:self.messageLabel];
[self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(4);
make.bottom.equalTo(self.contentView).offset(-4);
make.right.equalTo(self.contentView).offset(-12);
make.width.lessThanOrEqualTo(self.contentView).multipliedBy(0.7);
make.height.greaterThanOrEqualTo(@36);
}];
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bubbleView).offset(8);
make.bottom.equalTo(self.bubbleView).offset(-8);
make.left.equalTo(self.bubbleView).offset(12);
make.right.equalTo(self.bubbleView).offset(-12);
}];
}
- (void)configureWithMessage:(KBChatMessage *)message {
self.messageLabel.text = message.text ?: @"";
}
- (void)prepareForReuse {
[super prepareForReuse];
self.messageLabel.text = @"";
}
#pragma mark - Lazy
- (UIView *)bubbleView {
if (!_bubbleView) {
_bubbleView = [[UIView alloc] init];
_bubbleView.backgroundColor = [UIColor colorWithHex:0x02BEAC];
_bubbleView.layer.cornerRadius = 12;
_bubbleView.layer.masksToBounds = YES;
}
return _bubbleView;
}
- (UILabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.numberOfLines = 0;
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.textColor = [UIColor whiteColor];
_messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
return _messageLabel;
}
@end