// // TFSearchBar.m // TFReader // // Created by 谢腾飞 on 2020/12/24. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFSearchBar.h" #define SearchBar_Height 44 #define SearchBar_Width 50 @interface TFSearchBar () @property (nonatomic ,strong) UITextField *searchField; @end @implementation TFSearchBar - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; [self createSubViews]; } return self; } - (void)createSubViews { self.searchField = [[UITextField alloc] initWithFrame:CGRectZero]; self.searchField.backgroundColor = kColorRGBA(237, 238, 238, 1); self.searchField.clearButtonMode = UITextFieldViewModeWhileEditing; self.searchField.font = kMainFont; self.searchField.delegate = self; self.searchField.textColor = kBlackColor; self.searchField.returnKeyType = UIReturnKeySearch; [self.searchField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged]; self.searchField.layer.cornerRadius = 4; self.searchField.clipsToBounds = YES; [self addSubview:self.searchField]; [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kHalfMargin); make.top.mas_equalTo(5); make.right.mas_equalTo(self.mas_right).with.offset(- 2 * kHalfMargin - SearchBar_Width); make.height.mas_equalTo(SearchBar_Height - 10); }]; UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; leftImageView.image = [[UIImage imageNamed:@"public_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; leftImageView.tintColor = kColorRGBA(156, 155, 157, 1); UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, SearchBar_Height)]; leftView.backgroundColor = [UIColor clearColor]; [leftView addSubview:leftImageView]; leftImageView.center = leftView.center; self.searchField.leftViewMode = UITextFieldViewModeAlways; self.searchField.leftView = leftView; UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; cancelButton.backgroundColor = [UIColor clearColor]; [cancelButton setTitle:TFLocalizedString(@"取消") forState:UIControlStateNormal]; [cancelButton setTitleColor:kGrayTextColor forState:UIControlStateNormal]; [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside]; [cancelButton.titleLabel setFont:kMainFont]; [self addSubview:cancelButton]; [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.mas_right).with.offset(- kHalfMargin); make.centerY.equalTo(self.searchField); make.height.mas_equalTo(self.mas_height); make.width.mas_equalTo(SearchBar_Width); }]; } - (void)cancelClick { if ([self.delegate respondsToSelector:@selector(cancelButtonClicked)]) { [self.delegate cancelButtonClicked]; } } - (void)setPlaceholderText:(NSString *)placeholderText { _placeholderText = placeholderText; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName:kColorRGBA(156, 155, 157, 1),NSFontAttributeName:kMainFont}]; self.searchField.attributedPlaceholder = attrString; } - (void)setSearchText:(NSString *)searchText { _searchText = searchText; self.searchField.text = searchText; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // 如果为回车则将键盘收起 if ([string isEqualToString:@"\n"]) { if (textField.text.length > 0) { if ([self.delegate respondsToSelector:@selector(searchButtonClickedWithSearchText:)]) { [self.delegate searchButtonClickedWithSearchText:self.searchField.text]; } } else if (_placeholderText.length > 0) { if ([self.delegate respondsToSelector:@selector(searchButtonClickedWithSearchText:)]) { [self.delegate searchButtonClickedWithSearchText:_placeholderText]; } } [textField resignFirstResponder]; return NO; } return YES; } - (void)changeText:(UITextField *)sender { if ([sender.text isEqualToString:@""]) { if ([self.delegate respondsToSelector:@selector(searchBarCleanText)]) { [self.delegate searchBarCleanText]; } } } - (void)searchBarResignFirstResponder { if ([self.searchField isFirstResponder]) { [self.searchField resignFirstResponder]; } } @end