123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- //
- // TDSearchBar.m
- // TheoryNetwork
- //
- // Created by tederen on 2019/9/23.
- // Copyright © 2019 tederen. All rights reserved.
- //
- #import "TDSearchBar.h"
- @implementation TDSearchBar
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundImage = [UIImage new];
- self.placeholder = @" 搜索";
- self.layer.cornerRadius = 18.f;
- self.layer.borderWidth = 0.5;
- self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
- UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
- [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
- for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
- if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
- UITextField *textField = (UITextField *)view;
- //设置输入框的背景颜色
- textField.clipsToBounds = YES;
- //设置输入字体颜色
- textField.textColor = kColorFromRGB(0x0a0a0a);
- //设置默认文字颜色
- textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
- NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
- [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, self.placeholder.length)];
- [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, self.placeholder.length)];
- textField.attributedPlaceholder = placeholder;
- }
- }
- UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
- [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
- }
- return self;
- }
- - (instancetype)init
- {
- self = [super init];
- // if (self) {
- // self.backgroundImage = [UIImage new];
- // self.placeholder = @" 搜索";
- // self.layer.cornerRadius = 5.f;
- // self.layer.borderWidth = 0.5;
- // self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
- // UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
- // [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
- // for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
- // if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
- // UITextField *textField = (UITextField *)view;
- // //设置输入框的背景颜色
- // textField.clipsToBounds = YES;
- // //设置输入字体颜色
- // textField.textColor = kColorFromRGB(0x333333);
- // //设置默认文字颜色
- //
- // textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
- // }
- // }
- // UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
- // [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
- // }
- if (self) {
- self.backgroundImage = [UIImage new];
- self.placeholder = @" 搜索";
- self.layer.cornerRadius = 18.f;
- self.layer.borderWidth = 0.5;
- self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
- UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
- [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
- for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
- if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
- UITextField *textField = (UITextField *)view;
- //设置输入框的背景颜色
- textField.clipsToBounds = YES;
- //设置输入字体颜色
- textField.textColor = kColorFromRGB(0x0a0a0a);
- //设置默认文字颜色
- textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
- NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
- [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, self.placeholder.length)];
- [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, self.placeholder.length)];
- textField.attributedPlaceholder = placeholder;
- }
- }
- UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
- [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
- }
- return self;
- }
- - (void)setCusPlaceholder:(NSString *)text
- {
- for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
- if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
- UITextField *textField = (UITextField *)view;
- //设置输入框的背景颜色
- textField.clipsToBounds = YES;
- //设置输入字体颜色
- textField.textColor = kColorFromRGB(0x0a0a0a);
- //设置默认文字颜色
- textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
- NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:text];
- [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, text.length)];
- [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, text.length)];
- textField.attributedPlaceholder = placeholder;
- }
- }
- }
- @end
|