微博项目中,有个自定义搜索框
主要功能还是继承自UITextField,只是将个属性进行了封装
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 背景
self.background = [UIImage imageWithNamedResizable:@"searchbar_textfield_background"];
// 左侧搜索Icon
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageWithNamed:@"searchbar_textfield_search_icon"]];
iconView.bounds = CGRectMake(0, 0, 30, 15);
iconView.contentMode = UIViewContentModeCenter;
self.leftView = iconView;
self.leftViewMode = UITextFieldViewModeAlways;
// 提醒文字
NSMutableDictionary *atts = [[NSMutableDictionary alloc] init];
[atts setValue:[UIColor grayColor] forKey:NSForegroundColorAttributeName];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"待得花开虫破茧,我必化蝶向天飞" attributes:atts];
// 文字大小
self.font = [UIFont systemFontOfSize:13];
// 清除按钮
self.clearButtonMode = UITextFieldViewModeAlways;
// 设置返回按钮的类型
self.returnKeyType = UIReturnKeySearch;
// 设置键盘返回按钮是否可以点击
self.enablesReturnKeyAutomatically = YES;
}
return self;
}