| | |
| | | |
| | | #import "SearchNavView.h" |
| | | |
| | | @interface SearchNavView () |
| | | @interface SearchNavView () <UITextFieldDelegate> |
| | | |
| | | @property (nonatomic, strong) UIView *viewNav; |
| | | @property (nonatomic, strong) UIView *viewSearch; |
| | |
| | | - (instancetype)initWithFrame:(CGRect)frame { |
| | | self = [super initWithFrame:frame]; |
| | | if (self) { |
| | | self.backgroundColor = [UIColor whiteColor]; |
| | | [self setupViewConfig]; |
| | | } |
| | | return self; |
| | |
| | | [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 { |
| | |
| | | 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]; |
| | | } |
| | | } |
| | | |
| | |
| | | _textField.font = [UIFont systemFontOfSize:14]; |
| | | _textField.textAlignment = NSTextAlignmentLeft; |
| | | _textField.textColor = [UIColor blackColor]; |
| | | _textField.returnKeyType = UIReturnKeySearch;//变为搜索按钮 |
| | | _textField.delegate = self; |
| | | _textField.clearButtonMode = UITextFieldViewModeAlways; |
| | | } |
| | | return _textField; |
| | | } |