平时我们都是用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

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3.  
  4. typedef void(^SearchTextBlock)(NSString * searchText);
  5.  
  6. typedef NS_ENUM(NSUInteger, LGLSearchBarStyle) {
  7.  
  8. LGLSearchBarStyleDefault,
  9. LGLSearchBarStyleProminent,
  10. LGLSearchBarStyleMinimal
  11. };
  12.  
  13. typedef NS_ENUM(NSInteger, LGLTextBorderStyle) {
  14.  
  15. LGLTextBorderStyleNone,
  16. LGLTextBorderStyleLine,
  17. LGLTextBorderStyleBezel,
  18. LGLTextBorderStyleRoundedRect
  19. };
  20.  
  21. @interface LGLSearchBar : UIView
  22.  
  23. - (instancetype)initWithFrame:(CGRect)frame searchBarStyle:(LGLSearchBarStyle)style;
  24.  
  25. /** 提示文字 */
  26. @property (nonatomic, copy) NSString * placeholder;
  27.  
  28. /** 提示文字的颜色 */
  29. @property (nonatomic, strong) UIColor * placeholderColor;
  30.  
  31. /** 搜索框(输入框除外)的背景颜色 */
  32. @property (nonatomic, strong) UIColor * barBackgroudColor;
  33.  
  34. /** 输入框的背景颜色 */
  35. @property (nonatomic, strong) UIColor * textBackgroudColor;
  36.  
  37. /** 输入文字的颜色文字的颜色 */
  38. @property (nonatomic, strong) UIColor * textColor;
  39.  
  40. /** “搜索“文字的颜色 */
  41. @property (nonatomic, strong) UIColor * tintColor;
  42.  
  43. /** 提示文字的大小 */
  44. @property (nonatomic, assign) CGFloat placeholderFontSize;
  45.  
  46. /** 输入文字的大小 */
  47. @property (nonatomic, assign) CGFloat textFontSize;
  48.  
  49. /** 输入框的风格 */
  50. @property (nonatomic, assign) LGLTextBorderStyle textBordStyle;
  51.  
  52. @property (nonatomic, copy) SearchTextBlock block;
  53.  
  54. /** 获得搜索的Text */
  55. - (void)searchBarTextSearchTextBlock:(SearchTextBlock)block;
  56.  
  57. /** 改变里面输入框的 边框宽度 颜色 圆角 */
  58. - (void)setSearchBarBordeWidth:(CGFloat)Width bordColor:(UIColor *)bordColor bordRadius:(CGFloat)bordcornerRadius;
  59.  
  60. /**
  61. * 修改放大镜的图片
  62. * @pramas imageName 图片名称
  63. * @pramas scale 改变图片的大小
  64. *
  65. */
  66. - (void)setSearchBarImage:(NSString *)imageName scale:(CGFloat)scale;
  67.  
  68. @end

LGLSearchBar.m

  1. #import "LGLSearchBar.h"
  2.  
  3. @interface LGLSearchBar ()<UISearchBarDelegate>
  4.  
  5. {
  6. UISearchBar * _searchBar;
  7. }
  8.  
  9. @end
  10.  
  11. @implementation LGLSearchBar
  12.  
  13. /*
  14. 还需要添加 修改放大镜的图片
  15.  
  16. */
  17.  
  18. - (instancetype)initWithFrame:(CGRect)frame searchBarStyle:(LGLSearchBarStyle)style {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  22. _searchBar.barStyle = (style == LGLSearchBarStyleDefault) ? UISearchBarStyleDefault : ((style == LGLSearchBarStyleProminent) ? UISearchBarStyleProminent : UISearchBarStyleMinimal);
  23. _searchBar.placeholder = @"请输入搜索内容";
  24.  
  25. /*
  26. UITextAutocapitalizationTypeNone, 除非自己点击大写,否则永不大写
  27. UITextAutocapitalizationTypeWords, 以单词来区分,每个单词首字母大写
  28. UITextAutocapitalizationTypeSentences, 以句子来区分
  29. UITextAutocapitalizationTypeAllCharacters, 所有字母全部大写
  30. */
  31. _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
  32. //这个是透视属性
  33. //_searchBar.translucent = YES;
  34. _searchBar.delegate = self;
  35. [self addSubview:_searchBar];
  36. }
  37. return self;
  38. }
  39.  
  40. #pragma mark ==== UITextFeildDElegate =======
  41. // return NO to not resign first responder
  42. - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
  43. return YES;
  44. }
  45.  
  46. // called when keyboard search button pressed
  47. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  48. self.block(searchBar.text);
  49. searchBar.text = nil;
  50. [searchBar resignFirstResponder];
  51. }
  52.  
  53. // called when cancel button pressed
  54. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  55. self.block(searchBar.text);
  56. searchBar.text = nil;
  57. [searchBar resignFirstResponder];
  58. }
  59.  
  60. // 重新设置 searchbar cancel按钮 为搜索
  61. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
  62. searchBar.showsCancelButton = YES;
  63. for(UIView *view in [[[searchBar subviews] objectAtIndex:0] subviews]) {
  64. if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
  65. UIButton * cancel =(UIButton *)view;
  66. [cancel setTitle:@"搜索" forState:UIControlStateNormal];
  67. cancel.titleLabel.font = [UIFont systemFontOfSize:14];
  68. }
  69. }
  70. }
  71.  
  72. #pragma mark ==== 重写set方法 ===============
  73.  
  74. - (void)setPlaceholderFontSize:(CGFloat )placeholderFontSize {
  75. _placeholderFontSize = placeholderFontSize;
  76. [[self searchBarTextFeild] setValue:[UIFont systemFontOfSize:placeholderFontSize] forKeyPath:@"_placeholderLabel.font"];
  77. }
  78.  
  79. - (void)setTextFontSize:(CGFloat)textFontSize {
  80. _textFontSize = textFontSize;
  81. [self searchBarTextFeild].font = [UIFont systemFontOfSize:textFontSize];
  82. }
  83.  
  84. - (void)setPlaceholder:(NSString *)placeholder {
  85. _placeholder = placeholder;
  86. _searchBar.placeholder = placeholder;
  87. }
  88.  
  89. - (void)setTintColor:(UIColor *)tintColor {
  90. _tintColor = tintColor;
  91. _searchBar.tintColor = tintColor;
  92. }
  93.  
  94. - (void)setBarBackgroudColor:(UIColor *)barBackgroudColor {
  95. _barBackgroudColor = barBackgroudColor;
  96. _searchBar.barTintColor = barBackgroudColor;
  97. }
  98.  
  99. - (void)setTextBackgroudColor:(UIColor *)textBackgroudColor {
  100. _textBackgroudColor = textBackgroudColor;
  101. [[[_searchBar.subviews firstObject]subviews]lastObject].backgroundColor = textBackgroudColor;
  102. }
  103.  
  104. - (void)setPlaceholderColor:(UIColor *)placeholderColor {
  105. _placeholderColor = placeholderColor;
  106. [[self searchBarTextFeild] setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
  107. }
  108.  
  109. - (void)setTextColor:(UIColor *)textColor {
  110. _textColor = textColor;
  111. [self searchBarTextFeild].textColor = textColor;
  112. }
  113.  
  114. - (void)setSearchBarImage:(NSString *)imageName scale:(CGFloat)scale {
  115. UIImageView * leftView = [[UIImageView alloc] initWithImage:[self changeImageSize:imageName scale:scale]];
  116. [self searchBarTextFeild].leftView = leftView;
  117. }
  118.  
  119. - (void)setTextBordStyle:(LGLTextBorderStyle)textBordStyle {
  120. _textBordStyle = textBordStyle;
  121. UITextBorderStyle bordStyle = (textBordStyle == LGLTextBorderStyleNone) ? UITextBorderStyleNone : ((textBordStyle == LGLTextBorderStyleLine) ? UITextBorderStyleLine : ((textBordStyle == LGLTextBorderStyleBezel) ? UITextBorderStyleBezel : UITextBorderStyleRoundedRect));
  122. [self searchBarTextFeild].borderStyle = bordStyle;
  123. }
  124.  
  125. - (void)setSearchBarBordeWidth:(CGFloat)Width bordColor:(UIColor *)bordColor bordRadius:(CGFloat)bordcornerRadius {
  126. UITextField * texFeiled = [self searchBarTextFeild];
  127. texFeiled.layer.borderWidth = Width;
  128. texFeiled.layer.borderColor = bordColor.CGColor;
  129. texFeiled.layer.masksToBounds = YES;
  130. texFeiled.layer.cornerRadius = bordcornerRadius;
  131.  
  132. }
  133.  
  134. - (void)searchBarTextSearchTextBlock:(SearchTextBlock)block {
  135. self.block = block;
  136. }
  137.  
  138. // 修改图片的大小
  139. - (UIImage *)changeImageSize:(NSString *)imageName scale:(CGFloat)scale
  140. {
  141. NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:imageName]);
  142. UIImage * image = [UIImage imageWithData:imgData scale:scale];
  143. //声明使用自定义的图片
  144. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  145. return image;
  146. }
  147. //获取输入框
  148. - (UITextField *)searchBarTextFeild {
  149. UITextField * texFeild = nil;
  150. for (UIView* subview in [[_searchBar.subviews lastObject] subviews]) {
  151.  
  152. if ([subview isKindOfClass:[UITextField class]]) {
  153. texFeild = (UITextField*)subview;
  154.  
  155. } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
  156. {
  157. [subview removeFromSuperview];
  158. }
  159. }
  160. return texFeild;
  161. }
  162. @end

感谢大家的支持!

LGLSearchBar的更多相关文章

随机推荐

  1. LoadRunner11.52发布,全新的VTS

    LoadRunner11.52发布,全新的VTShttp://automationqa.com/forum.php?mod=viewthread&tid=2252&fromuid=2 ...

  2. POJ 1816 Wild Words

    Wild Words Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4412   Accepted: 1149 Descri ...

  3. IIS7/IIS7.5 二级域名伪静态设置方法

    转载地址:http://www.admin5.com/article/20120107/402582.shtml

  4. python网站收集

    1.python核心编程 习题答案    http://www.cnblogs.com/balian/category/279009.html 2.编程语言入门经典100例(python版)  htt ...

  5. CCF推荐国际学术会议

    类别如下计算机系统与高性能计算,计算机网络,网络与信息安全,软件工程,系统软件与程序设计语言,数据库.数据挖掘与内容检索,计算机科学理论,计算机图形学与多媒体,人工智能与模式识别,人机交互与普适计算, ...

  6. xfire框架内部基本结构解析

    1 概述 xfire是webservice的一个实现框架,是apache旗下CXF的前身,是一个比较被广泛使用的webservice框架,网上有很多关于如何使用xfire或cxf的hello worl ...

  7. wow 各职业体验(pvp)

    玩过职业 近战 武器战,冰DK,惩戒骑,增强萨,踏风 法系远程 鸟德,痛苦术,火法,奥法 治疗 奶德,奶骑,奶萨 三板甲职业就冰DK 最轻松,增强萨操作最频繁 机动性最好就武器战,踏风最差的,踏风群攻 ...

  8. VS2008简体中文正式版序列号-试用到期解决

    VS2008简体中文正式版序列号 VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition:XMQ2Y-4T3V6-XJ48Y-D3K2V- ...

  9. WCF小白初试 错误之一:“有零个应用程序终结点”的解决办法

    遇到这类问题 应该是配置文件出现了问题 解决办法是将配置文件中的<service name="命名空间+类名">就可以解决

  10. mysql 线上not in查询中的一个坑

    今天早上开发又过来说,怎么有个语句一直没有查询出结果,数据是有的呀,并发来了如下的sql(为了方法说明,表名及查询均做了修改): select * from t2 where t2.course no ...