177 lines
4.8 KiB
Objective-C
177 lines
4.8 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) 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 whiteColor];
|
|
|
|
[self addSubview:self.topLine];
|
|
[self addSubview:self.avatarImageView];
|
|
[self addSubview:self.containerView];
|
|
[self.containerView addSubview:self.textField];
|
|
[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.avatarImageView.mas_right).offset(10);
|
|
make.right.equalTo(self.sendButton.mas_left).offset(-10);
|
|
make.centerY.equalTo(self);
|
|
make.height.mas_equalTo(36);
|
|
}];
|
|
|
|
[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);
|
|
}];
|
|
}
|
|
|
|
#pragma mark - Public Methods
|
|
|
|
- (void)setPlaceholder:(NSString *)placeholder {
|
|
_placeholder = placeholder;
|
|
self.textField.placeholder = placeholder;
|
|
}
|
|
|
|
- (void)clearText {
|
|
self.textField.text = @"";
|
|
[self updateSendButtonState];
|
|
}
|
|
|
|
#pragma mark - Actions
|
|
|
|
- (void)sendButtonTapped {
|
|
NSString *text = self.textField.text;
|
|
if (text.length > 0 && self.onSend) {
|
|
self.onSend(text);
|
|
}
|
|
}
|
|
|
|
- (void)textFieldDidChange:(UITextField *)textField {
|
|
[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;
|
|
}
|
|
|
|
#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.backgroundColor = [UIColor systemGray6Color];
|
|
_containerView.layer.cornerRadius = 18;
|
|
_containerView.layer.masksToBounds = YES;
|
|
}
|
|
return _containerView;
|
|
}
|
|
|
|
- (UITextField *)textField {
|
|
if (!_textField) {
|
|
_textField = [[UITextField alloc] init];
|
|
_textField.placeholder = @"说点什么...";
|
|
_textField.font = [UIFont systemFontOfSize:14];
|
|
_textField.delegate = self;
|
|
_textField.returnKeyType = UIReturnKeySend;
|
|
[_textField addTarget:self
|
|
action:@selector(textFieldDidChange:)
|
|
forControlEvents:UIControlEventEditingChanged];
|
|
}
|
|
return _textField;
|
|
}
|
|
|
|
- (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.enabled = NO;
|
|
_sendButton.alpha = 0.5;
|
|
[_sendButton addTarget:self
|
|
action:@selector(sendButtonTapped)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _sendButton;
|
|
}
|
|
|
|
@end
|