平时我们都是用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的更多相关文章

随机推荐

  1. [ITSEC]信息安全·Web安全培训第一期客户端安全之UBB系列

    缩略图: 引文: 所谓UBB代码,是指论坛中的替代HTML代码的安全代码.ubb发帖编辑器 这种代码使用正则表达式来进行匹配,不同的论坛所使用的UBB代码很可能不同,不能一概而论.UBB代码的出现,使 ...

  2. Unity不同平台生成中预处理的注意点

    http://blog.csdn.net/pandawuwyj/article/details/7959335 Unity3D的项目,这周吃亏在宏上了.大背景是项目需要在Unity中用Hudson自动 ...

  3. Unix sed实用教程系列目录[已完成]

    本系列文章已经译完了,译自awk-sed@theunixschool,收获颇丰,作者没有讲明白的我做了补充,并且尝试讲的更清楚,整理成系列索引,方便学习,开篇非译文,是我为了方便后面的理解写的一篇,感 ...

  4. Codeforces Round #195 A B C 三题合集 (Div. 2)

    A 题 Vasily the Bear and Triangle 题目大意 一个等腰直角三角形 ABC,角 ACB 是直角,AC=BC,点 C 在原点,让确定 A 和 B 的坐标,使得三角形包含一个矩 ...

  5. 二十四、【开源】EFW框架Winform前端开发之项目结构说明和调试方法

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...

  6. Django:Model的Filter

    转自:http://www.douban.com/note/301166150/   django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct   1.多表 ...

  7. WCF 遇到的问题

    1.只有项目的net版本2.0以上的才可以引用到wcf的类库 2.HTTP 错误 404.17 - Not Found  映射问题   WCF服务建立好,提示这个错误,缺少映射问题,要将应用程序池和项 ...

  8. 解决访问StackOverFlow太慢的问题

    Stackoverflow加载时访问了被屏蔽的站点ajax.googleapis.com,导致加载缓慢,把这个站点加到Hosts里,指向127.0.0.1即可

  9. 笔记——Visual Studio 程序员箴言

    记录了一些感觉比较用得到的tips用于随时查看.要想看完整的的内容还是阅读<Visual Studio 程序员箴言>,不过有些内容我在新版本的VS里没能实现,或者有替代方案了. 避免意外复 ...

  10. vmware workstation11+centos7+lnmp一键安装包 环境搭建

    vmware workstation11 1.下载:http://pan.baidu.com/s/1gecipOJ 2.安装:直接下一步. centos7 1.下载:网易镜像 http://mirro ...