Files
keyboard/keyBoard/Class/AiTalk/V/Comment/KBAICommentInputView.m
2026-02-02 21:25:28 +08:00

220 lines
6.1 KiB
Objective-C

//
// KBAICommentInputView.m
// keyBoard
//
// Created by Mac on 2026/1/16.
//
#import "KBAICommentInputView.h"
#import <Masonry/Masonry.h>
@interface KBAICommentInputView () <UITextFieldDelegate>
@property(nonatomic, strong) UIView *containerView;
//@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;
@end
@implementation KBAICommentInputView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
}
return self;
}
#pragma mark - UI Setup
- (void)setupUI {
self.backgroundColor = [UIColor colorWithHex:0x797979 alpha:0.49];
[self addSubview:self.topLine];
// [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) {
make.left.right.top.equalTo(self);
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.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(12);
make.right.equalTo(self.sendButton.mas_left).offset(-12);
make.centerY.equalTo(self);
make.height.mas_equalTo(52);
}];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.containerView).offset(12);
make.right.equalTo(self.containerView).offset(-12);
make.centerY.equalTo(self.containerView);
}];
[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self).offset(-12);
make.centerY.equalTo(self);
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.placeholderLabel.text = placeholder;
[self updatePlaceholderVisibility];
}
- (void)clearText {
self.textField.text = @"";
[self updatePlaceholderVisibility];
[self updateSendButtonState];
}
- (void)showKeyboard {
[self.textField becomeFirstResponder];
}
#pragma mark - Actions
- (void)sendButtonTapped {
NSString *text = self.textField.text;
if (text.length > 0 && self.onSend) {
self.onSend(text);
}
}
- (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;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self sendButtonTapped];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self updatePlaceholderVisibility];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self updatePlaceholderVisibility];
}
#pragma mark - Lazy Loading
- (UIView *)topLine {
if (!_topLine) {
_topLine = [[UIView alloc] init];
_topLine.backgroundColor = [UIColor separatorColor];
}
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;
//}
- (UIView *)containerView {
if (!_containerView) {
_containerView = [[UIView alloc] init];
// _containerView.layer.cornerRadius = 26;
// _containerView.layer.masksToBounds = YES;
}
return _containerView;
}
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_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 setImage:[UIImage imageNamed:@"ai_sendmessage_icon"] forState:UIControlStateNormal];
_sendButton.enabled = NO;
// _sendButton.alpha = 0.5;
[_sendButton addTarget:self
action:@selector(sendButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
}
return _sendButton;
}
#pragma mark - Private
- (void)updatePlaceholderVisibility {
BOOL hasText = self.textField.text.length > 0;
self.placeholderLabel.hidden = hasText;
}
@end