//
|
// SearchNavView.m
|
// BuWanVideo2.0
|
//
|
// Created by Aeline on 2021/5/30.
|
// Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "SearchNavView.h"
|
|
@interface SearchNavView () <UITextFieldDelegate>
|
|
@property (nonatomic, strong) UIView *viewNav;
|
@property (nonatomic, strong) UIView *viewSearch;
|
|
@property (nonatomic, nullable, strong) UITextField *textField;
|
@end
|
|
@implementation SearchNavView
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
self = [super initWithFrame:frame];
|
if (self) {
|
self.backgroundColor = [UIColor whiteColor];
|
[self setupViewConfig];
|
}
|
return self;
|
}
|
|
- (void)setupViewConfig {
|
[self addSubview:self.viewNav];
|
self.viewNav.sd_layout.topSpaceToView(self, kStatusBarH).leftEqualToView(self).rightEqualToView(self).heightIs(44);
|
|
UIButton *buttonCancel = [[UIButton alloc] init];
|
[buttonCancel setTitle:@"取消" forState:UIControlStateNormal];
|
[buttonCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
buttonCancel.titleLabel.font = [UIFont systemFontOfSize:14];
|
[self addSubview:buttonCancel];
|
buttonCancel.sd_layout.rightSpaceToView(self, 20).centerYEqualToView(_viewNav).widthIs(30).heightIs(30);
|
[buttonCancel addTarget:self action:@selector(touchCacel) forControlEvents:UIControlEventTouchUpInside];
|
|
[self.viewNav addSubview:self.viewSearch];
|
self.viewSearch.sd_layout.leftSpaceToView(self.viewNav, 20).centerYEqualToView(self.viewNav).rightSpaceToView(buttonCancel, 21).heightIs(34);
|
self.viewSearch.sd_cornerRadius = @17;
|
|
UIImageView *imageViewSearch = [[UIImageView alloc] init];
|
imageViewSearch.image = [UIImage imageNamed:@"home_search"];
|
[_viewSearch addSubview:imageViewSearch];
|
imageViewSearch.sd_layout.leftSpaceToView(_viewSearch, 13).centerYEqualToView(_viewSearch).widthIs(19).heightIs(19);
|
|
[self.viewSearch addSubview:self.textField];
|
|
self.textField.sd_layout.leftSpaceToView(imageViewSearch, 8).centerYEqualToView(self.viewSearch).rightSpaceToView(self.viewSearch, 0).heightIs(34);
|
|
[self.textField addTarget:self action:@selector(searchValueChanged:) forControlEvents:UIControlEventEditingChanged];//添加实时的检测值事件
|
}
|
|
- (void)touchCacel {
|
!self.onCacel?:self.onCacel();
|
}
|
|
- (void)setTitle:(NSString *)title {
|
_title = title;
|
if (title) {
|
NSString *string = title;
|
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:string];
|
[placeholder addAttribute:NSForegroundColorAttributeName
|
value:UICOLOR_FROM_RGB(0x787878, 1.0)
|
range:NSMakeRange(0, string.length)];
|
_textField.attributedPlaceholder = placeholder;
|
}
|
}
|
|
- (void)setText:(NSString *)text {
|
_text = text;
|
if (text) {
|
NSString *string = @"请输入搜索内容";
|
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:string];
|
[placeholder addAttribute:NSForegroundColorAttributeName
|
value:UICOLOR_FROM_RGB(0x787878, 1.0)
|
range:NSMakeRange(0, string.length)];
|
_textField.attributedPlaceholder = placeholder;
|
_textField.text = text;
|
}
|
}
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
NSString *text = textField.text;
|
if (_title) {
|
if ([text isEqualToString:@""] || !text) {
|
text = textField.placeholder;
|
}
|
}
|
|
if (_delegate && [_delegate respondsToSelector:@selector(textFieldSearch:)]) {
|
[_delegate textFieldSearch:text];
|
}
|
return YES;
|
}
|
|
- (void)searchValueChanged:(UITextField *)textField {
|
if ([_delegate respondsToSelector:@selector(textFieldValueChange:)]) {
|
[self.delegate textFieldValueChange:textField.text];
|
}
|
}
|
|
- (UIView *)viewNav {
|
if (!_viewNav) {
|
_viewNav = [[UIView alloc] init];
|
}
|
return _viewNav;
|
}
|
|
- (UIView *)viewSearch {
|
if (!_viewSearch) {
|
_viewSearch = [[UIView alloc] init];
|
_viewSearch.backgroundColor = UICOLOR_FROM_RGB(0xEBEBEB, 1);
|
}
|
return _viewSearch;
|
}
|
|
- (UITextField *)textField {
|
if (!_textField) {
|
_textField = [[UITextField alloc] init];
|
_textField.font = [UIFont systemFontOfSize:14];
|
_textField.textAlignment = NSTextAlignmentLeft;
|
_textField.textColor = [UIColor blackColor];
|
_textField.returnKeyType = UIReturnKeySearch;//变为搜索按钮
|
_textField.delegate = self;
|
_textField.clearButtonMode = UITextFieldViewModeAlways;
|
}
|
return _textField;
|
}
|
@end
|