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的更多相关文章
随机推荐
- [转]jQuery.Autocomplete实现自动完成功能(详解)
本篇文章除了介绍jquery.autocomplete基本参数外,主要说明jquery.autocomplete的数据源的格式问题. 1.jquery.autocomplete参考地址 htt ...
- Debug Assertion Failed! Expression: _pFirstBlock == pHead
点击Abort之后,查看调用栈,发现异常在函数return时被时产生,进一步看是vector的析构函数被调用时产生,以前没开发过C++项目,没什么经验,这个错误让我很困惑,第一,我电脑上并没有f盘:第 ...
- [整理]C#反射(Reflection)详解
本人理解: 装配件:Assembly(程序集) 晚绑定:后期绑定 MSDN:反射(C# 编程指南) -----------------原文如下-------- 1. 什么是反射2. 命名空间与装配件的 ...
- C primer plus 练习题 第二章
6. #include <stdio.h> void echo(); int main() { /* echo(); echo(); echo(); printf("\n&quo ...
- [转]Python格式化输出
今天写程序又记不清格式化输出细节了……= =索性整理一下. python print格式化输出. 1. 打印字符串 print ("His name is %s"%("A ...
- Eclipse中用Logcat调试程序
调试程序的一种方法是用Logcat程序,在Eclipse中windows->show view->other->android->logcat可打开. 然后程序加入androi ...
- wow 各职业体验(pvp)
玩过职业 近战 武器战,冰DK,惩戒骑,增强萨,踏风 法系远程 鸟德,痛苦术,火法,奥法 治疗 奶德,奶骑,奶萨 三板甲职业就冰DK 最轻松,增强萨操作最频繁 机动性最好就武器战,踏风最差的,踏风群攻 ...
- java中String byte HexString的转换
原文:http://blog.sina.com.cn/s/blog_62e9ec530101ebv6.html HexString——>byte public static byte[] hex ...
- 受限玻尔兹曼机(RBM)学习笔记(四)对数似然函数
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...
- Swift_3.0_开启注释功能
1. Swift_3.0 没法快捷键(command+/)注释的原因:这个是因为苹果解决xcode ghost,把插件屏蔽了. 2. 解决办法: (1) 终端输入: sudo /usr/libexec ...