.h
#import <UIKit/UIKit.h> @class YJPSearchBar_View; @protocol YJPSearchBarDelegate <NSObject> @optional -(BOOL)searchBarShouldBeginEditing:(YJPSearchBar_View *)searchBar; -(void)searchBarTextDidBeginEditing:(YJPSearchBar_View *)searchBar; -(BOOL)searchBarShouldEndEditing:(YJPSearchBar_View *)searchBar; -(void)searchBarTextDidEndEditing:(YJPSearchBar_View *)searchBar; -(void)searchBar:(YJPSearchBar_View *)searchBar textDidChange:(NSString *)searchText; -(void)searchBarSearchButtonClicked:(YJPSearchBar_View *)searchBar;//确定按钮 -(void)searchBarCancelButtonClicked:(YJPSearchBar_View *)searchBar; @end /*************************************************************************/ @interface YJPSearchBar_View : UIView //文本的颜色 @property (nonatomic,strong) UIColor *textColor; //字体 @property (nonatomic,strong) UIFont *searchBarFont; //内容 @property (nonatomic,strong) NSString *text; //背景颜色 @property (nonatomic,strong) UIColor *searchBarColor; //默认文本 @property (nonatomic,strong) NSString *placeHolder; //默认文本的颜色 @property (nonatomic,strong) UIColor *placeHoldersColor; //默认文本字体大小 @property (nonatomic,strong) UIFont *placeHolderFont; //是否弹出键盘 @property (nonatomic,assign) BOOL isBecomeFirstResponder; //设置右边按钮的样式 @property (nonatomic,strong) UIImage *deleteImage; //设置代理 @property (nonatomic,weak) id<YJPSearchBarDelegate>delegate;.m
#import "YJPSearchBar_View.h" @interface YJPSearchBar_View ()<UITextFieldDelegate> @property (nonatomic,strong) UITextField *textField; @property (nonatomic,strong) UIImageView *leftView; @property (nonatomic,strong) UIButton *deleteBtn; @end @implementation YJPSearchBar_View -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initAllViews]; } return self; } -(void)initAllViews { self.layer.cornerRadius = self.frame.size.height / 2; self.layer.masksToBounds = YES; if (_leftView == nil) { _leftView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.frame.size.height - 17) / 2.0f, 17, 17)]; _leftView.image = [UIImage imageNamed:@"leftViewPhoto.jpg"]; [self addSubview:_leftView]; } if (_textField == nil) { _textField = [[UITextField alloc] initWithFrame:CGRectMake(_leftView.frame.size.width + _leftView.frame.origin.x + 10, 2, self.frame.size.width - (_leftView.frame.size.width + _leftView.frame.origin.x + 10) - (self.frame.size.height - 4), self.frame.size.height - 4)]; _textField.backgroundColor = [UIColor yellowColor]; _textField.returnKeyType = UIReturnKeyDone; _textField.delegate = self; [_textField addTarget:self action:@selector(textFieldValueChange:) forControlEvents:UIControlEventEditingChanged]; [self addSubview:_textField]; } if (_deleteBtn == nil) { _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _deleteBtn.frame = CGRectMake(self.frame.size.width - self.frame.size.height - 4, 2, self.frame.size.height - 4, self.frame.size.height - 4); [_deleteBtn setImage:[UIImage imageNamed:@"deleteBtnPhoto.jpg"] forState:UIControlStateNormal]; [_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_deleteBtn]; } }相关代理(Custom)
-(void)setIsBecomeFirstResponder:(BOOL)isBecomeFirstResponder { _isBecomeFirstResponder = isBecomeFirstResponder; if (_isBecomeFirstResponder) { [_textField becomeFirstResponder]; } }-(void)setSearchBarColor:(UIColor *)searchBarColor { _searchBarColor = searchBarColor; self.backgroundColor = _searchBarColor; _textField.backgroundColor = _searchBarColor; _leftView.backgroundColor = _searchBarColor; _deleteBtn.backgroundColor = _searchBarColor; }-(void)setSearchBarFont:(UIFont *)searchBarFont { _searchBarFont = searchBarFont; _textField.font = _searchBarFont; }-(void)setTextColor:(UIColor *)textColor { _textColor = textColor; _textField.textColor = textColor; }-(void)setPlaceHolder:(NSString *)placeHolder { _placeHolder = placeHolder; _textField.placeholder = _placeHolder; }-(void)setPlaceHoldersColor:(UIColor *)placeHoldersColor { _placeHoldersColor = placeHoldersColor; [_textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; }-(void)setPlaceHolderFont:(UIFont *)placeHolderFont { _placeHolderFont = placeHolderFont; [_textField setValue:_placeHolderFont forKeyPath:@"_placeholderLabel.font"]; }-(void)setText:(NSString *)text { _text = text; _textField.text = _text; }-(void)setDeleteImage:(UIImage *)deleteImage { _deleteImage = deleteImage; [_deleteBtn setImage:_deleteImage forState:UIControlStateNormal]; }#pragma mark - - deleteClick -(void)deleteClick:(UIButton *)delete { /** * 删除 */ if (_delegate && [_delegate respondsToSelector:@selector(searchBarCancelButtonClicked:)]) { [self.delegate searchBarCancelButtonClicked:self]; } }#pragma mark - - textFieldValueChange -(void)textFieldValueChange:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBar:textDidChange:)]) { [self.delegate searchBar:self textDidChange:textField.text]; } }#pragma mark - - UITextFieldDelegate -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) { [self.delegate searchBarShouldBeginEditing:self]; } return YES; }-(void)textFieldDidBeginEditing:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidBeginEditing:)]) { [self.delegate searchBarTextDidBeginEditing:self]; } }-(BOOL)textFieldShouldEndEditing:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidEndEditing:)]) { [self.delegate searchBarShouldEndEditing:self]; } return YES; }-(void)textFieldDidEndEditing:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBarTextDidEndEditing:)]) { [self.delegate searchBarTextDidEndEditing:self]; } }-(BOOL)textFieldShouldReturn:(UITextField *)textField { if (_delegate && [_delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) { [self.delegate searchBarSearchButtonClicked:self]; } return YES; }/* ****** [在ViewController_ViewDidLoad中调用:] ******* YJPSearchBar_View *searchBar = [[YJPSearchBar_View alloc] initWithFrame:CGRectMake(20, 80, self.view.frame.size.width - 40, 40)]; searchBar.placeHolder = @"搜索"; searchBar.searchBarColor = [UIColor colorWithRed:205 / 255.0 green:235 / 255.0 blue:206 / 255.0 alpha:1.0f]; searchBar.textColor = [UIColor whiteColor]; searchBar.searchBarFont = [UIFont systemFontOfSize:15.0]; searchBar.placeHoldersColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0f]; searchBar.placeHolderFont = [UIFont systemFontOfSize:15.0f]; searchBar.delegate = self; [self.view addSubview:searchBar]; */@end
作者:Jesonr
链接:http://www.jianshu.com/p/0eaeae75ffe3
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

被折叠的 条评论
为什么被折叠?



