//
//  KKSearchBar.m
//  Open
//
//  Created by 雷俊博 on 2020/7/8.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "KKSearchBar.h"

@interface KKSearchBar ()<UITextFieldDelegate>

@property (nonatomic, strong)UIImageView* searchIcon;

@property (nonatomic, strong)UITextField* textField;


@end

@implementation KKSearchBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.searchIcon];
        [self addSubview:self.textField];
        [self layoutConfig];
    }
    return self;
}

- (void)layoutConfig
{
    [self.searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.offset(20 );
        make.width.height.offset(14);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchIcon.mas_right).offset(2);
        make.top.right.bottom.offset(0);
    }];
}


#pragma mark - getters and setters
- (UIImageView *)searchIcon
{
    if (!_searchIcon) {
        _searchIcon = [[UIImageView alloc]init];
        _searchIcon.image = [UIImage imageNamed:@"icon_search"];
    }
    return _searchIcon;
}
- (UITextField *)textField
{
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.placeholder = @"请输入您要输入的内容";
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.userInteractionEnabled = NO;
        
        
         if (@available(iOS 13.0,*)) {
             NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请输入您要输入的内容" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0x999999]}];
                    
                    _textField.attributedPlaceholder = placeholderFont;
         }else{
              _textField.placeholder = @"请输入您要输入的内容";
              [_textField setValue:[UIFont systemFontOfSize:14.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
              [_textField setValue:[UIColor colorWithHex:0x999999] forKeyPath:@"_placeholderLabel.textColor"];
         }
    }
    return _textField;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
