修改bug
This commit is contained in:
@@ -73,6 +73,7 @@
|
||||
self.messageLabel.numberOfLines = 0;
|
||||
self.messageLabel.font = [UIFont systemFontOfSize:16];
|
||||
self.messageLabel.textColor = [UIColor whiteColor];
|
||||
self.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
// 设置 preferredMaxLayoutWidth 让 AutoLayout 能正确计算多行高度
|
||||
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width * 0.75 - 16 - 24;
|
||||
self.messageLabel.preferredMaxLayoutWidth = maxWidth;
|
||||
@@ -94,20 +95,22 @@
|
||||
make.center.equalTo(self.voiceButton);
|
||||
}];
|
||||
|
||||
// bubbleView 约束
|
||||
// 关键修复:bubbleView 必须有明确的高度约束链
|
||||
[self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.voiceButton.mas_bottom).offset(4);
|
||||
make.bottom.equalTo(self.contentView).offset(-4);
|
||||
make.bottom.equalTo(self.contentView).offset(-4).priority(999); // 降低优先级避免冲突
|
||||
make.left.equalTo(self.contentView).offset(16);
|
||||
make.width.lessThanOrEqualTo(self.contentView).multipliedBy(0.75);
|
||||
}];
|
||||
|
||||
// messageLabel 约束
|
||||
// 关键修复:messageLabel 约束必须完整,让 AutoLayout 能推导出 bubbleView 的高度
|
||||
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.bubbleView).offset(10);
|
||||
make.bottom.equalTo(self.bubbleView).offset(-10);
|
||||
make.bottom.equalTo(self.bubbleView).offset(-10).priority(999); // 降低优先级
|
||||
make.left.equalTo(self.bubbleView).offset(12);
|
||||
make.right.equalTo(self.bubbleView).offset(-12);
|
||||
// 关键修复:给 messageLabel 一个最小高度,防止高度为 0
|
||||
make.height.greaterThanOrEqualTo(@20);
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -127,10 +130,18 @@
|
||||
NSLog(@"[KBChatAssistantMessageCell] 启动新的打字机效果");
|
||||
[self startTypewriterEffectWithText:message.text];
|
||||
} else {
|
||||
// 直接显示完整文本
|
||||
// 关键修复:直接显示完整文本时,清除 attributedText,使用普通 text
|
||||
NSLog(@"[KBChatAssistantMessageCell] 直接显示完整文本(needsTypewriter: %d, isComplete: %d)",
|
||||
message.needsTypewriterEffect, message.isComplete);
|
||||
self.messageLabel.text = message.text;
|
||||
self.messageLabel.attributedText = nil; // 清除 attributedText
|
||||
self.messageLabel.text = message.text; // 设置普通文本
|
||||
|
||||
// 强制布局更新
|
||||
[self.contentView setNeedsLayout];
|
||||
[self.contentView layoutIfNeeded];
|
||||
|
||||
NSLog(@"[KBChatAssistantMessageCell] 直接显示后 Label frame: %@",
|
||||
NSStringFromCGRect(self.messageLabel.frame));
|
||||
}
|
||||
|
||||
// 格式化语音时长(如果时长为 0,不显示)
|
||||
@@ -191,30 +202,33 @@
|
||||
self.fullText = text;
|
||||
self.currentCharIndex = 0;
|
||||
|
||||
// 先设置完整文本,让布局系统计算出正确的高度
|
||||
// 关键修复:先设置普通文本,让布局系统计算出正确的高度
|
||||
self.messageLabel.text = text;
|
||||
|
||||
// 强制布局更新
|
||||
[self.messageLabel setNeedsLayout];
|
||||
[self.bubbleView setNeedsLayout];
|
||||
// 强制布局更新,确保 Cell 有正确的高度
|
||||
[self.contentView setNeedsLayout];
|
||||
[self layoutIfNeeded];
|
||||
[self.contentView layoutIfNeeded];
|
||||
|
||||
NSLog(@"[KBChatAssistantMessageCell] 布局后 Label frame: %@", NSStringFromCGRect(self.messageLabel.frame));
|
||||
NSLog(@"[KBChatAssistantMessageCell] 布局后 Label frame: %@, bubbleView frame: %@",
|
||||
NSStringFromCGRect(self.messageLabel.frame),
|
||||
NSStringFromCGRect(self.bubbleView.frame));
|
||||
|
||||
// 使用 NSAttributedString 实现打字机效果
|
||||
// 先把所有文字设置为透明,然后逐个显示
|
||||
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;
|
||||
|
||||
// 确保在主线程创建定时器
|
||||
// 关键修复:布局完成后再应用打字机效果的 attributedText
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
// 使用 NSAttributedString 实现打字机效果
|
||||
// 先把所有文字设置为透明,然后逐个显示
|
||||
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;
|
||||
|
||||
// 再次强制布局,确保 attributedText 不会改变高度
|
||||
[self.contentView layoutIfNeeded];
|
||||
|
||||
// 每 0.03 秒显示一个字符(更快的打字机效果)
|
||||
self.typewriterTimer = [NSTimer scheduledTimerWithTimeInterval:0.03
|
||||
target:self
|
||||
|
||||
Reference in New Issue
Block a user