al
liurenjie
2021-07-03 1b73d5d0b495f2d643c2523e1b8399f925b4f702
BuWanVideo2.0/SearchNavView.m
@@ -8,7 +8,7 @@
#import "SearchNavView.h"
@interface SearchNavView ()
@interface SearchNavView () <UITextFieldDelegate>
@property (nonatomic, strong) UIView *viewNav;
@property (nonatomic, strong) UIView *viewSearch;
@@ -21,6 +21,7 @@
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupViewConfig];
    }
    return self;
@@ -50,6 +51,8 @@
    [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 {
@@ -65,6 +68,39 @@
                            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];
    }
}
@@ -89,6 +125,9 @@
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.textColor = [UIColor blackColor];
        _textField.returnKeyType = UIReturnKeySearch;//变为搜索按钮
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeAlways;
    }
    return _textField;
}