LGLSearchBar
平时我们都是用UITextFeild 来写搜索框, 最近有时间就自己重新封装了UISearchBar, 他可以自行修改里面的属性来达到我们使用的要求。
源代码下载地址:https://github.com/liguoliangiOS/LGLSearchBar.git 欢迎各位下载使用,有问题可以qq联系我 185226139 如果觉得好用请给我点赞,谢谢!
这个使用简单:
#import "LGLSearchBar.h"
LGLSearchBar * searchBar = [[LGLSearchBar alloc] initWithFrame:CGRectMake(10, 200, SCREENWIDTH - 20, 40) searchBarStyle:LGLSearchBarStyleDefault];
[searchBar searchBarTextSearchTextBlock:^(NSString *searchText) {
NSLog(@"%@", searchText);
}];
下面上具体的代码
LGLSearchBar.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> typedef void(^SearchTextBlock)(NSString * searchText); typedef NS_ENUM(NSUInteger, LGLSearchBarStyle) { LGLSearchBarStyleDefault,
LGLSearchBarStyleProminent,
LGLSearchBarStyleMinimal
}; typedef NS_ENUM(NSInteger, LGLTextBorderStyle) { LGLTextBorderStyleNone,
LGLTextBorderStyleLine,
LGLTextBorderStyleBezel,
LGLTextBorderStyleRoundedRect
}; @interface LGLSearchBar : UIView - (instancetype)initWithFrame:(CGRect)frame searchBarStyle:(LGLSearchBarStyle)style; /** 提示文字 */
@property (nonatomic, copy) NSString * placeholder; /** 提示文字的颜色 */
@property (nonatomic, strong) UIColor * placeholderColor; /** 搜索框(输入框除外)的背景颜色 */
@property (nonatomic, strong) UIColor * barBackgroudColor; /** 输入框的背景颜色 */
@property (nonatomic, strong) UIColor * textBackgroudColor; /** 输入文字的颜色文字的颜色 */
@property (nonatomic, strong) UIColor * textColor; /** “搜索“文字的颜色 */
@property (nonatomic, strong) UIColor * tintColor; /** 提示文字的大小 */
@property (nonatomic, assign) CGFloat placeholderFontSize; /** 输入文字的大小 */
@property (nonatomic, assign) CGFloat textFontSize; /** 输入框的风格 */
@property (nonatomic, assign) LGLTextBorderStyle textBordStyle; @property (nonatomic, copy) SearchTextBlock block; /** 获得搜索的Text */
- (void)searchBarTextSearchTextBlock:(SearchTextBlock)block; /** 改变里面输入框的 边框宽度 颜色 圆角 */
- (void)setSearchBarBordeWidth:(CGFloat)Width bordColor:(UIColor *)bordColor bordRadius:(CGFloat)bordcornerRadius; /**
* 修改放大镜的图片
* @pramas imageName 图片名称
* @pramas scale 改变图片的大小
*
*/
- (void)setSearchBarImage:(NSString *)imageName scale:(CGFloat)scale; @end
LGLSearchBar.m
#import "LGLSearchBar.h"
@interface LGLSearchBar ()<UISearchBarDelegate>
{
UISearchBar * _searchBar;
}
@end
@implementation LGLSearchBar
/*
还需要添加 修改放大镜的图片
*/
- (instancetype)initWithFrame:(CGRect)frame searchBarStyle:(LGLSearchBarStyle)style {
self = [super initWithFrame:frame];
if (self) {
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
_searchBar.barStyle = (style == LGLSearchBarStyleDefault) ? UISearchBarStyleDefault : ((style == LGLSearchBarStyleProminent) ? UISearchBarStyleProminent : UISearchBarStyleMinimal);
_searchBar.placeholder = @"请输入搜索内容";
/*
UITextAutocapitalizationTypeNone, 除非自己点击大写,否则永不大写
UITextAutocapitalizationTypeWords, 以单词来区分,每个单词首字母大写
UITextAutocapitalizationTypeSentences, 以句子来区分
UITextAutocapitalizationTypeAllCharacters, 所有字母全部大写
*/
_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
//这个是透视属性
//_searchBar.translucent = YES;
_searchBar.delegate = self;
[self addSubview:_searchBar];
}
return self;
}
#pragma mark ==== UITextFeildDElegate =======
// return NO to not resign first responder
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}
// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
self.block(searchBar.text);
searchBar.text = nil;
[searchBar resignFirstResponder];
}
// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
self.block(searchBar.text);
searchBar.text = nil;
[searchBar resignFirstResponder];
}
// 重新设置 searchbar cancel按钮 为搜索
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
searchBar.showsCancelButton = YES;
for(UIView *view in [[[searchBar subviews] objectAtIndex:0] subviews]) {
if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
UIButton * cancel =(UIButton *)view;
[cancel setTitle:@"搜索" forState:UIControlStateNormal];
cancel.titleLabel.font = [UIFont systemFontOfSize:14];
}
}
}
#pragma mark ==== 重写set方法 ===============
- (void)setPlaceholderFontSize:(CGFloat )placeholderFontSize {
_placeholderFontSize = placeholderFontSize;
[[self searchBarTextFeild] setValue:[UIFont systemFontOfSize:placeholderFontSize] forKeyPath:@"_placeholderLabel.font"];
}
- (void)setTextFontSize:(CGFloat)textFontSize {
_textFontSize = textFontSize;
[self searchBarTextFeild].font = [UIFont systemFontOfSize:textFontSize];
}
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = placeholder;
_searchBar.placeholder = placeholder;
}
- (void)setTintColor:(UIColor *)tintColor {
_tintColor = tintColor;
_searchBar.tintColor = tintColor;
}
- (void)setBarBackgroudColor:(UIColor *)barBackgroudColor {
_barBackgroudColor = barBackgroudColor;
_searchBar.barTintColor = barBackgroudColor;
}
- (void)setTextBackgroudColor:(UIColor *)textBackgroudColor {
_textBackgroudColor = textBackgroudColor;
[[[_searchBar.subviews firstObject]subviews]lastObject].backgroundColor = textBackgroudColor;
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
_placeholderColor = placeholderColor;
[[self searchBarTextFeild] setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
[self searchBarTextFeild].textColor = textColor;
}
- (void)setSearchBarImage:(NSString *)imageName scale:(CGFloat)scale {
UIImageView * leftView = [[UIImageView alloc] initWithImage:[self changeImageSize:imageName scale:scale]];
[self searchBarTextFeild].leftView = leftView;
}
- (void)setTextBordStyle:(LGLTextBorderStyle)textBordStyle {
_textBordStyle = textBordStyle;
UITextBorderStyle bordStyle = (textBordStyle == LGLTextBorderStyleNone) ? UITextBorderStyleNone : ((textBordStyle == LGLTextBorderStyleLine) ? UITextBorderStyleLine : ((textBordStyle == LGLTextBorderStyleBezel) ? UITextBorderStyleBezel : UITextBorderStyleRoundedRect));
[self searchBarTextFeild].borderStyle = bordStyle;
}
- (void)setSearchBarBordeWidth:(CGFloat)Width bordColor:(UIColor *)bordColor bordRadius:(CGFloat)bordcornerRadius {
UITextField * texFeiled = [self searchBarTextFeild];
texFeiled.layer.borderWidth = Width;
texFeiled.layer.borderColor = bordColor.CGColor;
texFeiled.layer.masksToBounds = YES;
texFeiled.layer.cornerRadius = bordcornerRadius;
}
- (void)searchBarTextSearchTextBlock:(SearchTextBlock)block {
self.block = block;
}
// 修改图片的大小
- (UIImage *)changeImageSize:(NSString *)imageName scale:(CGFloat)scale
{
NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:imageName]);
UIImage * image = [UIImage imageWithData:imgData scale:scale];
//声明使用自定义的图片
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
return image;
}
//获取输入框
- (UITextField *)searchBarTextFeild {
UITextField * texFeild = nil;
for (UIView* subview in [[_searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
texFeild = (UITextField*)subview;
} else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
}
}
return texFeild;
}
@end
感谢大家的支持!
LGLSearchBar的更多相关文章
随机推荐
- Unity 由Verlet数值积分产生的头发运动
先发下效果图. 参考项目unitychan-crs-master与miloyip大神的博客 爱丽丝的发丝,使用Verlet数值积分,根据旧的现在位置与上一桢位置来计算现在的位置,得到新的方向,上面的运 ...
- React Native ——实现一个简单的抓取github上的项目数据列表
/** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; var React ...
- CentOS 7 Vmware虚拟机 /root空间不足解决方法(使用gparted live)
1,关闭虚拟机,编辑虚拟机设置,增加虚拟磁盘的大小,我这里增加10GB 2,连接CDrom到ISO文件(gparted-live-0.19.0-1-i486.iso),使用gparted live启动 ...
- SSAS:OLE DB 错误: OLE DB 或 ODBC 错误 : Login failed for user 'NT Service\MSSQLServerOLAPService'
问题描述 按照微软官方教程尝试使用SSAS做OLAP时,出现如下错误信息: Severity Code Description Project File Line Suppression State ...
- iOS SDK Release Notes for iOS 9 iOS9 SDK 版本更新说明
Important: This is a preliminary document for an API or technology in development. Apple is supplyin ...
- wordpress表结构
WordPress仅仅用了10 个表:wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_term_relationships, ...
- 由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件
前两天安装了一堆补丁后突然发现,原本正常使用了一年的应用系统打不开了,到事件日志中发现有.net framewok 2.0的补丁安装失败的日志,于从从重装补丁开始.到重新注册.net框架,再到所有.n ...
- Git使用总结
一.Git的特性 Speed 速度(git是用c语言写的.一般都是提交到本地) Simple design Strong support for non-linear development (tho ...
- 转载:更换zImage中的initramfs
From: http://blog.csdn.net/linuxaxis/article/details/8769722 好吧,折腾了两三个星期,USB的问题没搞定,看来功夫还不到家,看了下efuse ...
- 使用MyXls,出现访问被拒绝情况
MyXls 作为一款比较好用的 操作Excel 的开源项目. 最近日常偶然发现报权限的问题,导出xls失败,处理方式以下: 如报上图所示错误,则需要在web.config中 <system.we ...