// // XPHomeSearchNavView.m // xplan-ios // // Created by 冯硕 on 2021/11/29. // #import "XPHomeSearchNavView.h" ///Third #import ///Tool #import "ThemeColor.h" #import "XPMacro.h" #import "UIButton+EnlargeTouchArea.h" @interface XPHomeSearchNavView () ///返回 @property (nonatomic,strong) UIButton *backButton; ///输入框背景 @property (nonatomic,strong) UIView * inputBackView; ///搜索logo @property (nonatomic,strong) UIImageView *searchImageView; ///输入框 @property (nonatomic,strong) UITextField *searchTextField; ///取消 @property (nonatomic,strong) UIButton *cancleButton; @end @implementation XPHomeSearchNavView - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.backButton]; [self addSubview:self.inputBackView]; [self addSubview:self.cancleButton]; [self.inputBackView addSubview:self.searchImageView]; [self.inputBackView addSubview:self.searchTextField]; } - (void)initSubViewConstraints { [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(22, 22)); make.left.mas_equalTo(self).offset(8); make.centerY.mas_equalTo(self.inputBackView); }]; [self.inputBackView mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(30); make.left.mas_equalTo(self.backButton.mas_right).offset(5); make.right.mas_equalTo(self.cancleButton.mas_left).offset(-13); make.top.mas_equalTo(self).offset(statusbarHeight + 10); }]; [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self).offset(-15); make.centerY.mas_equalTo(self.inputBackView); make.height.mas_equalTo(30); }]; [self.searchImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(18, 18)); make.centerY.mas_equalTo(self.inputBackView); make.left.mas_equalTo(self.inputBackView).offset(9); }]; [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.searchImageView.mas_right); make.right.top.bottom.mas_equalTo(self.inputBackView); }]; } #pragma mark - Event Response - (void)cancleButtonAction:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavView:didClickSearch:)]) { [self.delegate xPHomeSearchNavView:self didClickSearch:sender]; } } - (void)backButtonAction:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavView:didClickBack:)]) { [self.delegate xPHomeSearchNavView:self didClickBack:sender]; } } - (void)textFieldChange:(NSNotification *)notification { UITextField *textField = (UITextField *)notification.object; NSString *searchStr = textField.text; if (searchStr.length == 0) { if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchNavViewClearTextField:)]) { [self.delegate xPHomeSearchNavViewClearTextField:self]; } } } #pragma mark - Getters And Setters - (UIButton *)backButton { if (!_backButton) { _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateNormal]; [_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateSelected]; [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [_backButton setEnlargeEdgeWithTop:10 right:5 bottom:10 left:8]; } return _backButton; } - (UIView *)inputBackView { if (!_inputBackView) { _inputBackView = [[UIView alloc] init]; _inputBackView.backgroundColor = [ThemeColor appCellBackgroundColor]; _inputBackView.layer.masksToBounds = YES; _inputBackView.layer.cornerRadius = 15; } return _inputBackView; } - (UIImageView *)searchImageView { if (!_searchImageView) { _searchImageView = [[UIImageView alloc] init]; _searchImageView.userInteractionEnabled = YES; _searchImageView.image = [UIImage imageNamed:@"home_nav_search"]; } return _searchImageView; } - (UITextField *)searchTextField { if (!_searchTextField) { _searchTextField = [[UITextField alloc] init]; _searchTextField.layer.cornerRadius = 15; _searchTextField.layer.masksToBounds = YES; _searchTextField.tintColor = [ThemeColor secondTextColor]; _searchTextField.textColor = [ThemeColor mainTextColor]; _searchTextField.backgroundColor = [UIColor clearColor]; _searchTextField.font = [UIFont systemFontOfSize:13]; NSString *placeholder = [NSString stringWithFormat:@"搜索昵称/ID/房间名"]; _searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName : [ThemeColor secondTextColor]}]; _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; _searchTextField.returnKeyType = UIReturnKeySearch; _searchTextField.enablesReturnKeyAutomatically = YES; } return _searchTextField; } - (UIButton *)cancleButton { if (!_cancleButton) { _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_cancleButton setTitle:@"搜索" forState:UIControlStateNormal]; [_cancleButton setTitleColor:[ThemeColor secondTextColor] forState:UIControlStateNormal]; _cancleButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:16]; [_cancleButton addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [_cancleButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:0]; } return _cancleButton; } @end