aihome里统一发布消息控件

This commit is contained in:
2026-02-03 13:31:52 +08:00
parent 19cb29616f
commit 08628bcd1d

View File

@@ -18,10 +18,11 @@
#import "KBUserSessionManager.h"
#import "LSTPopView.h"
#import "KBAIMessageVC.h"
#import "KBAICommentInputView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
@interface KBAIHomeVC () <UICollectionViewDelegate, UICollectionViewDataSource, KBVoiceToTextManagerDelegate, KBVoiceRecordManagerDelegate, UIGestureRecognizerDelegate, KBChatLimitPopViewDelegate, UITextViewDelegate>
@interface KBAIHomeVC () <UICollectionViewDelegate, UICollectionViewDataSource, KBVoiceToTextManagerDelegate, KBVoiceRecordManagerDelegate, UIGestureRecognizerDelegate, KBChatLimitPopViewDelegate>
///
@property (nonatomic, strong) UICollectionView *collectionView;
@@ -37,16 +38,10 @@
@property (nonatomic, strong) UITapGestureRecognizer *dismissKeyboardTap;
@property (nonatomic, weak) LSTPopView *chatLimitPopView;
///
@property (nonatomic, strong) UIView *textInputContainerView;
///
@property (nonatomic, strong) UITextView *textInputTextView;
///
@property (nonatomic, strong) UIButton *sendButton;
///
@property (nonatomic, strong) UILabel *placeholderLabel;
///
@property (nonatomic, strong) MASConstraint *textInputContainerBottomConstraint;
///
@property (nonatomic, strong) KBAICommentInputView *commentInputView;
///
@property (nonatomic, strong) MASConstraint *commentInputBottomConstraint;
///
@property (nonatomic, assign) BOOL isTextInputMode;
@@ -116,8 +111,8 @@
if (!firstResponder) {
return NO;
}
// textInputTextView
if (firstResponder == self.textInputTextView) {
// commentInputView
if ([firstResponder isDescendantOfView:self.commentInputView]) {
return YES;
}
return [firstResponder isDescendantOfView:self.voiceInputBar];
@@ -179,7 +174,7 @@
#pragma mark - 1
- (void)setupUI {
self.voiceInputBarHeight = 80.0;
self.voiceInputBarHeight = 52;
self.baseInputBarBottomSpacing = KB_TABBAR_HEIGHT;
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -217,37 +212,13 @@
///
- (void)setupTextInputView {
//
[self.view addSubview:self.textInputContainerView];
[self.textInputContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
self.textInputContainerBottomConstraint = make.bottom.equalTo(self.view).offset(100); //
make.height.mas_greaterThanOrEqualTo(50);
}];
[self.textInputContainerView addSubview:self.textInputTextView];
[self.textInputContainerView addSubview:self.sendButton];
[self.textInputTextView addSubview:self.placeholderLabel];
[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.textInputContainerView).offset(-16);
make.bottom.equalTo(self.textInputContainerView).offset(-10);
make.width.mas_equalTo(60);
make.height.mas_equalTo(36);
}];
[self.textInputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.textInputContainerView).offset(16);
make.right.equalTo(self.sendButton.mas_left).offset(-10);
make.top.equalTo(self.textInputContainerView).offset(8);
make.bottom.equalTo(self.textInputContainerView).offset(-8);
make.height.mas_greaterThanOrEqualTo(36);
make.height.mas_lessThanOrEqualTo(100);
}];
[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.textInputTextView).offset(15);
make.top.equalTo(self.textInputTextView).offset(8);
//
[self.view addSubview:self.commentInputView];
[self.commentInputView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).offset(12);
make.right.equalTo(self.view).offset(-12);
self.commentInputBottomConstraint = make.bottom.equalTo(self.view).offset(100); //
make.height.mas_equalTo(self.voiceInputBarHeight);
}];
}
@@ -264,18 +235,17 @@
- (void)showTextInputView {
self.isTextInputMode = YES;
self.voiceInputBar.hidden = YES;
self.textInputContainerView.hidden = NO;
[self.textInputTextView becomeFirstResponder];
self.commentInputView.hidden = NO;
[self.commentInputView showKeyboard];
}
///
- (void)hideTextInputView {
self.isTextInputMode = NO;
[self.textInputTextView resignFirstResponder];
self.textInputContainerView.hidden = YES;
[self.view endEditing:YES];
[self.commentInputView clearText];
self.commentInputView.hidden = YES;
self.voiceInputBar.hidden = NO;
self.textInputTextView.text = @"";
self.placeholderLabel.hidden = NO;
}
#pragma mark - 2
@@ -585,11 +555,11 @@
bottomSpacing = keyboardHeight - 5.0;
//
if (self.isTextInputMode) {
[self.textInputContainerBottomConstraint setOffset:-keyboardHeight];
[self.commentInputBottomConstraint setOffset:-keyboardHeight];
}
} else {
bottomSpacing = self.baseInputBarBottomSpacing;
[self.textInputContainerBottomConstraint setOffset:100]; //
[self.commentInputBottomConstraint setOffset:100]; //
}
[self.voiceInputBarBottomConstraint setOffset:-bottomSpacing];
@@ -624,7 +594,7 @@
if ([touch.view isDescendantOfView:self.voiceInputBar]) {
return NO;
}
if ([touch.view isDescendantOfView:self.textInputContainerView]) {
if ([touch.view isDescendantOfView:self.commentInputView]) {
return NO;
}
return YES;
@@ -718,18 +688,6 @@
[pop pop];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
self.placeholderLabel.hidden = textView.text.length > 0;
//
CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)];
CGFloat newHeight = MIN(MAX(size.height, 36), 100);
[textView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_greaterThanOrEqualTo(newHeight);
}];
}
#pragma mark - Lazy Load
- (UICollectionView *)collectionView {
@@ -763,53 +721,19 @@
return _voiceInputBar;
}
- (UIView *)textInputContainerView {
if (!_textInputContainerView) {
_textInputContainerView = [[UIView alloc] init];
_textInputContainerView.backgroundColor = [UIColor whiteColor];
_textInputContainerView.hidden = YES;
- (KBAICommentInputView *)commentInputView {
if (!_commentInputView) {
_commentInputView = [[KBAICommentInputView alloc] init];
_commentInputView.layer.cornerRadius = 26;
_commentInputView.layer.masksToBounds = true;
_commentInputView.hidden = YES;
_commentInputView.placeholder = KBLocalized(@"send a message");
__weak typeof(self) weakSelf = self;
_commentInputView.onSend = ^(NSString *text) {
[weakSelf handleCommentInputSend:text];
};
}
return _textInputContainerView;
}
- (UITextView *)textInputTextView {
if (!_textInputTextView) {
_textInputTextView = [[UITextView alloc] init];
_textInputTextView.font = [UIFont systemFontOfSize:16];
_textInputTextView.textColor = [UIColor blackColor];
_textInputTextView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
_textInputTextView.layer.cornerRadius = 18;
_textInputTextView.layer.masksToBounds = YES;
_textInputTextView.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);
_textInputTextView.delegate = self;
_textInputTextView.returnKeyType = UIReturnKeySend;
_textInputTextView.enablesReturnKeyAutomatically = YES;
}
return _textInputTextView;
}
- (UIButton *)sendButton {
if (!_sendButton) {
_sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_sendButton setTitle:KBLocalized(@"send") forState:UIControlStateNormal];
[_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_sendButton.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
_sendButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.6 blue:1.0 alpha:1.0];
_sendButton.layer.cornerRadius = 18;
_sendButton.layer.masksToBounds = YES;
[_sendButton addTarget:self action:@selector(sendButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
return _sendButton;
}
- (UILabel *)placeholderLabel {
if (!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.text = KBLocalized(@"输入消息...");
_placeholderLabel.font = [UIFont systemFontOfSize:16];
_placeholderLabel.textColor = [UIColor lightGrayColor];
}
return _placeholderLabel;
return _commentInputView;
}
#pragma mark - KBChatLimitPopViewDelegate
@@ -876,22 +800,18 @@
[self.navigationController pushViewController:vc animated:YES];
}
/// - handleTranscribedText
- (void)sendButtonTapped {
NSString *text = [self.textInputTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (text.length == 0) {
/// - handleTranscribedText
- (void)handleCommentInputSend:(NSString *)text {
NSString *trimmedText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trimmedText.length == 0) {
return;
}
//
self.textInputTextView.text = @"";
self.placeholderLabel.hidden = NO;
//
[self hideTextInputView];
// handleTranscribedText
[self handleTranscribedText:text];
[self handleTranscribedText:trimmedText];
}
#pragma mark - KBVoiceToTextManagerDelegate