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的更多相关文章
随机推荐
- 【android原生态RPG游戏框架源码】
转载请注明原创地址:http://www.cnblogs.com/zisou/p/android-RPG.html 这份源码是在今年6月份写的,当时公司有一个技术部们的学习讨论的讲座,然后我自己就写了 ...
- 点击短信中的url打开某个应用
实现功能: 短信内容中含有url(例如,http://youngo.com/app/),点击后打开apk 遗留问题: 点击url后,会出现选择框,让用户选择是用浏览器打开还是用该apk打开----没有 ...
- SAP S4 Finance6个支持企业实时财务管理的主要创新领域
本文将讲述下 SAP Simple Finance里面6个支持企业实时财务管理的主要创新领域. Simple Finance 在以下几个方面具有自己独特的优势: ● 更加简洁的用户体验,可以让用户在任 ...
- Microsoft 2013 新技术学习笔记 二
在探讨系统重构的代码结构组织之前,先初步考虑框架与数据库的交互,在.net平台上数据访问方案有人总结为三类:DataSet.ADO.net 2.0.ORM组件.我只熟悉ADO.NET方式,众多的企业特 ...
- 使用 crosswalk-cordova 打包sencha touch 项目,再也不用担心安卓兼容问题!
国内的安卓手机品牌众多,安卓操作系统碎片化也很严重,我们使用sencha touch 开发的应用不可避免的出现了各种无解的兼容性问题. 有时候我就在想,有没有既能支持cordova,又能让我们把Chr ...
- springMVC对于controller处理方法返回值的可选类型
http://www.360doc.com/content/14/0309/19/834950_359081989.shtml
- Cloning EBS from Linux 5 to Linux 6 Fails: "Error While Loading Shared Libraries: libclntsh.so.10.1
SYMPTOMS During clone Oracle Applications R12 from Linux 5 to Linux 6 the following error occurs ...
- easyui datagrid to excel
$.extend($.fn.datagrid.methods, { toExcel: function(jq, filename){ return jq.each(function(){ var ur ...
- 基于MSP430F413水果电池供电的低功耗时钟
我最早接触MSP430时候,看到书的第一页就是一张水果电池的图片,一直以来想做一个低功耗的可以水果电池供电的系统,毕业之后的下半年选择MSP430F413单片机来画了一个低功耗的板子,一直没有调试 ...
- joomla3.1安装不通过Magic Quotes GPC解决方法
测试安装下joomla 3.1稳定版,但是不能成功,Magic Quotes GPC始终显示 否红色,这样就安装不了了! 要解决这个很简单,开启Magic Quotes GPC就行了,于是找到php. ...