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
//
//  YTHSearchTextField.m
//  PapayaPlayerDaqo
//
//  Created by 味口 on 15/8/12.
//  Copyright (c) 2015年 wgj. All rights reserved.
//
 
#import "YTHSearchTextField.h"
 
@implementation YTHSearchTextField
 
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        //添加视图
        [self submitViews];
    }
    return self;
}
 
#pragma mark 添加视图
- (void)submitViews{
    //外面视图
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    titleView.backgroundColor = YTHColor(34, 28, 25);
    //前面空置区域
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, self.frame.size.height)];
    view.backgroundColor = titleView.backgroundColor;
    //输入框
    UITextField *custom = [[UITextField alloc] initWithFrame:CGRectMake(view.frame.size.width, 0, titleView.frame.size.width-view.frame.size.width-25, titleView.frame.size.height)];
    [custom setTextColor:[UIColor whiteColor]];
    custom.delegate = self;
    custom.returnKeyType = UIReturnKeySearch;//把return键变为搜索
    custom.font = [UIFont systemFontOfSize:14];
    custom.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入搜索内容" attributes:@{NSForegroundColorAttributeName: KGlobalLightGreyColor_90}];
    [custom addTarget:self action:@selector(SearchValueChanged:)  forControlEvents:UIControlEventAllEditingEvents];//添加实时的检测值事件
    self.Field = custom;
    
    //搜索-清除按钮
    UIButton *clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(titleView.frame.size.width-30, 2.5, 25, 25)];
    [clearBtn setImage:[UIImage imageNamed:@"搜索-结果-清除"] forState:UIControlStateNormal];
    [clearBtn addTarget:self action:@selector(clearclick:) forControlEvents:UIControlEventTouchUpInside];
    clearBtn.hidden = YES;
    self.clearBtn = clearBtn;
    
    [titleView addSubview:view];
    [titleView addSubview:custom];
    [titleView addSubview:clearBtn];
    [self addSubview:titleView];
}
 
#pragma mark  实时检测值事件
- (void)SearchValueChanged:(UITextField *)Field{
    if (Field.text.length>0) {
        self.clearBtn.hidden = NO;
    }else{
        self.clearBtn.hidden = YES;
    }
    if ([self.delegate respondsToSelector:@selector(YTHSearchField:SearchValueChanged:)]) {
        [self.delegate YTHSearchField:self SearchValueChanged:Field];
    }
}
 
#pragma mark 点击清除按钮
- (void)clearclick:(UIButton *)btn{
    self.Field.text = @"";
    btn.hidden = YES;
    if ([self.delegate respondsToSelector:@selector(YTHSearchField:clearClik:)]) {
        [self.delegate YTHSearchField:self clearClik:btn];
    }
}
 
#pragma mark 输入框的协议
- (BOOL)textFieldShouldReturn:(UITextField *)textField{//点击了键盘的搜索
    if ([self.delegate respondsToSelector:@selector(YTHSearchField:textFieldShouldReturn:)]) {
        return [self.delegate YTHSearchField:self textFieldShouldReturn:textField];
    }
    return nil;
}
 
 
@end