//
|
// 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
|