CNWitnessSearchView.m 3.32 KB
//
//  CNWitnessSearchView.m
//  AFNetworking
//
//  Created by CNLive on 2022/6/15.
//

#import "CNWitnessSearchView.h"
@interface CNWitnessSearchView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *searchImage;
@property (nonatomic, strong) UIView *lineView;
@end

@implementation CNWitnessSearchView
- (instancetype)init
{
    if (self = [super init]) {
        [self addSubview:self.searchImage];
        [self addSubview:self.searchField];
        [self addSubview:self.lineView];
        [self.searchImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.width.offset(16);
            make.height.offset(16);
            make.centerY.equalTo(self);
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.searchImage.mas_right).offset(10);
            make.width.offset(0.5);
            make.height.offset(11);
            make.centerY.equalTo(self);
        }];
        [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView.mas_right).offset(10);
            make.right.equalTo(self).offset(-5);
            make.top.bottom.equalTo(self);
            make.centerY.equalTo(self);
        }];
       
    }
    return self;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (([textField.text isEqualToString:@""]&&![string isNotBlank]) ||textField.text.length > 20) {
        return NO;
    }
    if (textField.text.length + string.length > 20) {
        [QMUITips showError:@"搜索内容不能超过20个字符" inView:AppKeyWindow hideAfterDelay:2];
        return NO;
    }
    return YES;
}
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    if ([textField.text containsString:@" "]) {
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    !self.clearBlock?:self.clearBlock();
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField.text isEqualToString:@""] || !textField.text) {
        [QMUITips showWithText:@"请输入搜索内容" inView:AppKeyWindow hideAfterDelay:1.5];
        return NO;
    }
    !self.searchBlock?:self.searchBlock(textField.text);
    return YES;
}
#pragma mark - Setting&&Getting
- (UIImageView *)searchImage
{
    if (!_searchImage) {
        _searchImage = [[UIImageView alloc] init];
        [_searchImage setImage:[UIImage loadBundleImage:@"witness_search"]];
    }
    return _searchImage;
}
- (UITextField *)searchField
{
    if (!_searchField) {
        _searchField = [[UITextField alloc] init];
        _searchField.placeholder = @"请输入关键字";
        _searchField.delegate = self;
        _searchField.returnKeyType = UIReturnKeySearch;
        _searchField.clearButtonMode = UITextFieldViewModeAlways;
        _searchField.textColor = CNLiveColorWithHexString(@"#333333");
        _searchField.font = UIFontCNMake(14);
    }
    return _searchField;
}
- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = CNLiveColorWithHexString(@"#333333");
    }
    return _lineView;
}
@end