小说绘上架版本
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// TFSearchBar.h
|
||||
// TFReader
|
||||
//
|
||||
// Created by 谢腾飞 on 2020/12/24.
|
||||
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol TFSearchBarDelegate <NSObject>
|
||||
@optional
|
||||
|
||||
- (void)cancelButtonClicked;
|
||||
|
||||
- (void)searchButtonClickedWithSearchText:(NSString *)searchText;
|
||||
|
||||
- (void)searchBarCleanText;
|
||||
@end
|
||||
|
||||
@interface TFSearchBar : UIView<UITextFieldDelegate>
|
||||
|
||||
@property (nonatomic ,weak) id <TFSearchBarDelegate> delegate;
|
||||
@property (nonatomic ,copy) NSString *placeholderText;
|
||||
@property (nonatomic ,copy) NSString *searchText;
|
||||
|
||||
- (void)searchBarResignFirstResponder;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,136 @@
|
||||
//
|
||||
// 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
|
||||
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// TFSearchView.h
|
||||
// TFReader
|
||||
//
|
||||
// Created by 谢腾飞 on 2020/12/24.
|
||||
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface TFSearchView : UIView
|
||||
|
||||
@property (nonatomic ,strong) NSArray *placeholderArray;
|
||||
@property (nonatomic ,strong) NSString *currentPlaceholder;
|
||||
@property (nonatomic ,assign) BOOL searchViewDarkColor;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,134 @@
|
||||
//
|
||||
// TFSearchView.m
|
||||
// TFReader
|
||||
//
|
||||
// Created by 谢腾飞 on 2020/12/24.
|
||||
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TFSearchView.h"
|
||||
#import "WXYZ_GCDTimer.h"
|
||||
|
||||
@interface TFSearchView ()
|
||||
|
||||
@property (nonatomic ,strong) WXYZ_GCDTimer *timer;
|
||||
@property (nonatomic ,weak) UILabel *searchTitle;
|
||||
@property (nonatomic ,strong) UIImageView *searchIcon;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TFSearchView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
{
|
||||
if (self = [super initWithFrame:frame]) {
|
||||
|
||||
self.backgroundColor = kColorRGBA(255, 255, 255, 0.3);
|
||||
self.layer.borderColor = kColorRGBA(235, 235, 241, 1).CGColor;
|
||||
self.layer.borderWidth = 0;
|
||||
|
||||
[self createSubViews];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)createSubViews
|
||||
{
|
||||
self.searchIcon = [[UIImageView alloc] init];
|
||||
self.searchIcon.image = [[UIImage imageNamed:@"public_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
|
||||
self.searchIcon.userInteractionEnabled = YES;
|
||||
self.searchIcon.tintColor = kWhiteColor;
|
||||
[self addSubview:self.searchIcon];
|
||||
|
||||
[self.searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.right.mas_equalTo(self.mas_right).with.offset(- kHalfMargin);
|
||||
make.centerY.mas_equalTo(self.mas_centerY);
|
||||
make.width.height.mas_equalTo(self.mas_height).with.multipliedBy(0.7);
|
||||
}];
|
||||
|
||||
UILabel *searchTitle = [[UILabel alloc] init];
|
||||
searchTitle.backgroundColor = [UIColor clearColor];
|
||||
searchTitle.textColor = [UIColor whiteColor];
|
||||
searchTitle.font = kFont12;
|
||||
searchTitle.textAlignment = NSTextAlignmentLeft;
|
||||
[self addSubview:searchTitle];
|
||||
self.searchTitle = searchTitle;
|
||||
|
||||
[searchTitle mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.mas_equalTo(self.mas_left).with.offset(kHalfMargin);
|
||||
make.centerY.mas_equalTo(self.mas_centerY);
|
||||
make.right.mas_equalTo(self.searchIcon.mas_left).with.offset(- kHalfMargin);
|
||||
make.height.mas_equalTo(self.mas_height);
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)setSearchViewDarkColor:(BOOL)searchViewDarkColor
|
||||
{
|
||||
_searchViewDarkColor = searchViewDarkColor;
|
||||
|
||||
if (searchViewDarkColor) {
|
||||
self.searchIcon.tintColor = kGrayTextLightColor;
|
||||
self.backgroundColor = kColorRGBA(255, 255, 255, 0.0);
|
||||
self.searchTitle.textColor = kGrayTextLightColor;
|
||||
self.layer.borderColor = kColorRGBA(235, 235, 241, 1).CGColor;
|
||||
self.layer.borderWidth = 0.6;
|
||||
|
||||
} else {
|
||||
|
||||
self.searchIcon.tintColor = kWhiteColor;
|
||||
self.backgroundColor = kColorRGBA(255, 255, 255, 0.3);
|
||||
self.searchTitle.textColor = [UIColor whiteColor];
|
||||
self.layer.borderColor = kColorRGBA(235, 235, 241, 1).CGColor;
|
||||
self.layer.borderWidth = 0;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setPlaceholderArray:(NSArray *)placeholderArray
|
||||
{
|
||||
_placeholderArray = placeholderArray;
|
||||
|
||||
if (placeholderArray.count > 0) {
|
||||
self.searchTitle.text = [placeholderArray objectOrNilAtIndex:0];
|
||||
|
||||
if (placeholderArray.count > 1) {
|
||||
int __block index = 0;
|
||||
|
||||
WS(weakSelf)
|
||||
[self.timer stopTimer];
|
||||
[self.timer startTimer];
|
||||
|
||||
self.timer.timerRunningBlock = ^(NSUInteger runTimes, CGFloat currentTime) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
weakSelf.searchTitle.text = [placeholderArray objectOrNilAtIndex:index];
|
||||
|
||||
if (++ index == placeholderArray.count) {
|
||||
index = 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)currentPlaceholder
|
||||
{
|
||||
return self.searchTitle.text;
|
||||
}
|
||||
|
||||
- (WXYZ_GCDTimer *)timer
|
||||
{
|
||||
if (!_timer) {
|
||||
_timer = [[WXYZ_GCDTimer alloc] initCycleTimerWithInterval:10 immediatelyCallBack:YES];
|
||||
}
|
||||
return _timer;
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
[super layoutSubviews];
|
||||
|
||||
self.layer.cornerRadius = self.height / 2;
|
||||
self.clipsToBounds = YES;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user