admin
2023-04-21 0b3a4aaf99ea251bc8e27b96115288f0988fcffe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
//  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