优化发送输入框

This commit is contained in:
2026-02-02 21:25:28 +08:00
parent 6e50cdcd2a
commit 19cb29616f
9 changed files with 126 additions and 63 deletions

View File

@@ -1075,7 +1075,7 @@ static NSString * const KBChatSessionDidResetNotification = @"KBChatSessionDidRe
[self.popView dismiss];
}
CGFloat customViewHeight = KB_SCREEN_HEIGHT * 0.7;
CGFloat customViewHeight = KB_SCREEN_HEIGHT * 0.75;
KBAICommentView *customView = [[KBAICommentView alloc]
initWithFrame:CGRectMake(0, 0, KB_SCREEN_WIDTH, customViewHeight)];

View File

@@ -11,8 +11,9 @@
@interface KBAICommentInputView () <UITextFieldDelegate>
@property(nonatomic, strong) UIView *containerView;
@property(nonatomic, strong) UIImageView *avatarImageView;
//@property(nonatomic, strong) UIImageView *avatarImageView;
@property(nonatomic, strong) UITextField *textField;
@property(nonatomic, strong) UILabel *placeholderLabel;
@property(nonatomic, strong) UIButton *sendButton;
@property(nonatomic, strong) UIView *topLine;
@@ -31,12 +32,13 @@
#pragma mark - UI Setup
- (void)setupUI {
self.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor colorWithHex:0x797979 alpha:0.49];
[self addSubview:self.topLine];
[self addSubview:self.avatarImageView];
// [self addSubview:self.avatarImageView];
[self addSubview:self.containerView];
[self.containerView addSubview:self.textField];
[self addSubview:self.placeholderLabel];
[self addSubview:self.sendButton];
[self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -44,17 +46,17 @@
make.height.mas_equalTo(0.5);
}];
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(12);
make.centerY.equalTo(self);
make.width.height.mas_equalTo(32);
}];
// [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(self).offset(12);
// make.centerY.equalTo(self);
// make.width.height.mas_equalTo(32);
// }];
[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.avatarImageView.mas_right).offset(10);
make.right.equalTo(self.sendButton.mas_left).offset(-10);
make.left.equalTo(self).offset(12);
make.right.equalTo(self.sendButton.mas_left).offset(-12);
make.centerY.equalTo(self);
make.height.mas_equalTo(36);
make.height.mas_equalTo(52);
}];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -69,17 +71,23 @@
make.width.mas_equalTo(50);
make.height.mas_equalTo(30);
}];
[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
}];
}
#pragma mark - Public Methods
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = placeholder;
self.textField.placeholder = placeholder;
self.placeholderLabel.text = placeholder;
[self updatePlaceholderVisibility];
}
- (void)clearText {
self.textField.text = @"";
[self updatePlaceholderVisibility];
[self updateSendButtonState];
}
@@ -97,13 +105,14 @@
}
- (void)textFieldDidChange:(UITextField *)textField {
[self updatePlaceholderVisibility];
[self updateSendButtonState];
}
- (void)updateSendButtonState {
BOOL hasText = self.textField.text.length > 0;
self.sendButton.enabled = hasText;
self.sendButton.alpha = hasText ? 1.0 : 0.5;
// self.sendButton.alpha = hasText ? 1.0 : 0.5;
}
#pragma mark - UITextFieldDelegate
@@ -113,6 +122,15 @@
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self updatePlaceholderVisibility];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self updatePlaceholderVisibility];
}
#pragma mark - Lazy Loading
- (UIView *)topLine {
@@ -123,25 +141,24 @@
return _topLine;
}
- (UIImageView *)avatarImageView {
if (!_avatarImageView) {
_avatarImageView = [[UIImageView alloc] init];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
_avatarImageView.layer.cornerRadius = 16;
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.backgroundColor = [UIColor systemGray5Color];
_avatarImageView.image = [UIImage systemImageNamed:@"person.circle.fill"];
_avatarImageView.tintColor = [UIColor systemGray3Color];
}
return _avatarImageView;
}
//- (UIImageView *)avatarImageView {
// if (!_avatarImageView) {
// _avatarImageView = [[UIImageView alloc] init];
// _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
// _avatarImageView.layer.cornerRadius = 16;
// _avatarImageView.layer.masksToBounds = YES;
// _avatarImageView.backgroundColor = [UIColor systemGray5Color];
// _avatarImageView.image = [UIImage systemImageNamed:@"person.circle.fill"];
// _avatarImageView.tintColor = [UIColor systemGray3Color];
// }
// return _avatarImageView;
//}
- (UIView *)containerView {
if (!_containerView) {
_containerView = [[UIView alloc] init];
_containerView.backgroundColor = [UIColor systemGray6Color];
_containerView.layer.cornerRadius = 18;
_containerView.layer.masksToBounds = YES;
// _containerView.layer.cornerRadius = 26;
// _containerView.layer.masksToBounds = YES;
}
return _containerView;
}
@@ -149,27 +166,42 @@
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.placeholder = @"说点什么...";
_textField.textColor = [UIColor whiteColor];
_textField.textAlignment = NSTextAlignmentLeft;
_textField.font = [UIFont systemFontOfSize:14];
_textField.delegate = self;
_textField.returnKeyType = UIReturnKeySend;
[_textField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
[self updatePlaceholderVisibility];
}
return _textField;
}
- (UILabel *)placeholderLabel {
if (!_placeholderLabel) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.text = @"Send A Message";
_placeholderLabel.textColor = [UIColor whiteColor];
_placeholderLabel.font = [UIFont systemFontOfSize:14];
_placeholderLabel.textAlignment = NSTextAlignmentCenter;
_placeholderLabel.userInteractionEnabled = NO;
}
return _placeholderLabel;
}
- (UIButton *)sendButton {
if (!_sendButton) {
_sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_sendButton setTitle:@"发送" forState:UIControlStateNormal];
[_sendButton setTitleColor:[UIColor systemBlueColor]
forState:UIControlStateNormal];
_sendButton.titleLabel.font = [UIFont systemFontOfSize:15
weight:UIFontWeightMedium];
// [_sendButton setTitle:@"发送" forState:UIControlStateNormal];
// [_sendButton setTitleColor:[UIColor systemBlueColor]
// forState:UIControlStateNormal];
// _sendButton.titleLabel.font = [UIFont systemFontOfSize:15
// weight:UIFontWeightMedium];
[_sendButton setImage:[UIImage imageNamed:@"ai_sendmessage_icon"] forState:UIControlStateNormal];
_sendButton.enabled = NO;
_sendButton.alpha = 0.5;
// _sendButton.alpha = 0.5;
[_sendButton addTarget:self
action:@selector(sendButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
@@ -177,4 +209,11 @@
return _sendButton;
}
#pragma mark - Private
- (void)updatePlaceholderVisibility {
BOOL hasText = self.textField.text.length > 0;
self.placeholderLabel.hidden = hasText;
}
@end

View File

@@ -189,8 +189,8 @@ static NSString *const kCommentFooterIdentifier = @"CommentFooter";
}];
[self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.height.mas_equalTo(50);
make.left.right.equalTo(self).inset(12);
make.height.mas_equalTo(52);
self.inputBottomConstraint =
make.bottom.equalTo(self).offset(-KB_SafeAreaBottom());
}];
@@ -846,8 +846,9 @@ static NSInteger const kRepliesLoadCount = 5;
- (KBAICommentInputView *)inputView {
if (!_inputView) {
_inputView = [[KBAICommentInputView alloc] init];
_inputView.placeholder = @"说点什么...";
_inputView.placeholder = @"Send A Message";
_inputView.layer.cornerRadius = 26;
_inputView.clipsToBounds = true;
__weak typeof(self) weakSelf = self;
_inputView.onSend = ^(NSString *text) {
[weakSelf sendCommentWithText:text];