CNWitnessSearchView.m
3.32 KB
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
//
// CNWitnessSearchView.m
// AFNetworking
//
// Created by CNLive on 2022/6/15.
//
#import "CNWitnessSearchView.h"
@interface CNWitnessSearchView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *searchImage;
@property (nonatomic, strong) UIView *lineView;
@end
@implementation CNWitnessSearchView
- (instancetype)init
{
if (self = [super init]) {
[self addSubview:self.searchImage];
[self addSubview:self.searchField];
[self addSubview:self.lineView];
[self.searchImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(15);
make.width.offset(16);
make.height.offset(16);
make.centerY.equalTo(self);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.searchImage.mas_right).offset(10);
make.width.offset(0.5);
make.height.offset(11);
make.centerY.equalTo(self);
}];
[self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.lineView.mas_right).offset(10);
make.right.equalTo(self).offset(-5);
make.top.bottom.equalTo(self);
make.centerY.equalTo(self);
}];
}
return self;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (([textField.text isEqualToString:@""]&&![string isNotBlank]) ||textField.text.length > 20) {
return NO;
}
if (textField.text.length + string.length > 20) {
[QMUITips showError:@"搜索内容不能超过20个字符" inView:AppKeyWindow hideAfterDelay:2];
return NO;
}
return YES;
}
- (void)textFieldDidChangeSelection:(UITextField *)textField{
if ([textField.text containsString:@" "]) {
textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
!self.clearBlock?:self.clearBlock();
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([textField.text isEqualToString:@""] || !textField.text) {
[QMUITips showWithText:@"请输入搜索内容" inView:AppKeyWindow hideAfterDelay:1.5];
return NO;
}
!self.searchBlock?:self.searchBlock(textField.text);
return YES;
}
#pragma mark - Setting&&Getting
- (UIImageView *)searchImage
{
if (!_searchImage) {
_searchImage = [[UIImageView alloc] init];
[_searchImage setImage:[UIImage loadBundleImage:@"witness_search"]];
}
return _searchImage;
}
- (UITextField *)searchField
{
if (!_searchField) {
_searchField = [[UITextField alloc] init];
_searchField.placeholder = @"请输入关键字";
_searchField.delegate = self;
_searchField.returnKeyType = UIReturnKeySearch;
_searchField.clearButtonMode = UITextFieldViewModeAlways;
_searchField.textColor = CNLiveColorWithHexString(@"#333333");
_searchField.font = UIFontCNMake(14);
}
return _searchField;
}
- (UIView *)lineView
{
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = CNLiveColorWithHexString(@"#333333");
}
return _lineView;
}
@end