SearchBarDisplayCenter.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // SearchBarDisplayCenter.m
  3. // KunShanETDZ
  4. //
  5. // Created by 云联智慧 on 2019/5/20.
  6. // Copyright © 2019 云联智慧. All rights reserved.
  7. //
  8. #import "SearchBarDisplayCenter.h"
  9. @interface SearchBarDisplayCenter ()<UITextFieldDelegate>
  10. /** 搜索标志 */
  11. @property (nonatomic,strong) UIImageView *seachIconIV;
  12. /** 底部view */
  13. @property (nonatomic,strong) UIView *containerView;
  14. /** 输入框 */
  15. @property (nonatomic,strong) UITextField *searchField;
  16. /** 提示 */
  17. @property (nonatomic,strong) UILabel *placeholderLabel;
  18. /** 盛装提示语和搜索标志的view */
  19. @property (nonatomic,strong) UIView *placeholderView;
  20. /** 记录居中时候搜索提示view的x坐标 */
  21. @property(nonatomic,assign)CGFloat searchX;
  22. @end
  23. @implementation SearchBarDisplayCenter
  24. -(instancetype)initWithFrame:(CGRect)frame
  25. {
  26. if(self = [super initWithFrame:frame])
  27. {
  28. self.backgroundColor = [UIColor clearColor];
  29. _containerView = [[UIView alloc]init];
  30. _containerView.backgroundColor = [UIColor clearColor];
  31. [self addSubview:_containerView];
  32. _searchField = [[UITextField alloc]init];
  33. _searchField.delegate = self;
  34. _searchField.backgroundColor = [UIColor clearColor];
  35. [_containerView addSubview:_searchField];
  36. _placeholderView = [[UIView alloc]init];
  37. _placeholderView.backgroundColor = [UIColor clearColor];
  38. [_containerView addSubview:_placeholderView];
  39. _placeholderLabel = [[UILabel alloc]init];
  40. [_placeholderView addSubview:_placeholderLabel];
  41. _seachIconIV = [[UIImageView alloc]init];
  42. [_placeholderView addSubview:_seachIconIV];
  43. [self addObserver];
  44. }
  45. return self;
  46. }
  47. #pragma mark -- 弹出键盘
  48. -(void)showKeyboard:(NSNotification *)notification
  49. {
  50. //键盘出现后的位置
  51. CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  52. CGFloat keyboardHeight = endFrame.size.height;
  53. if (keyboardHeight==0)
  54. {
  55. //解决搜狗输入法三次调用此方法的bug、
  56. // IOS8.0之后可以安装第三方键盘,如搜狗输入法之类的。
  57. // 获得的高度都为0.这是因为键盘弹出的方法:- (void)keyBoardWillShow:(NSNotification *)notification需要执行三次,你如果打印一下,你会发现键盘高度为:第一次:0;第二次:216:第三次:282.并不是获取不到高度,而是第三次才获取真正的高度.
  58. return;
  59. }
  60. //键盘弹起时的动画效果
  61. UIViewAnimationOptions option = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
  62. //键盘动画时长
  63. NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  64. __block typeof(self)weakSelf = self;
  65. [UIView animateWithDuration:duration delay:0 options:option animations:^{
  66. weakSelf.placeholderView.frame = CGRectMake(16.0, 0, weakSelf.placeholderView.frame.size.width, weakSelf.placeholderView.frame.size.height);
  67. } completion:nil];
  68. }
  69. #pragma mark --- 收起键盘
  70. -(void)hideKeyboard:(NSNotification *)notification
  71. {
  72. NSLog(@"收起键盘");
  73. UIViewAnimationOptions option = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
  74. NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  75. __weak typeof(self)weakSelf = self;
  76. [UIView animateWithDuration:duration delay:0 options:option animations:^{
  77. if(weakSelf.searchField.text.length == 0)
  78. {
  79. weakSelf.placeholderView.frame = CGRectMake(weakSelf.searchX, 0, weakSelf.placeholderView.frame.size.width, weakSelf.placeholderView.frame.size.height);
  80. }
  81. } completion:nil];
  82. }
  83. #pragma mark 移除监听
  84. /** 移除监听 */
  85. -(void)dealloc
  86. {
  87. NSLog(@"移除监听");
  88. [[NSNotificationCenter defaultCenter] removeObserver:self];
  89. }
  90. #pragma mark 添加监听
  91. /** 添加监听 */
  92. -(void)addObserver
  93. {
  94. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldContentDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
  95. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
  96. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
  97. }
  98. -(void)textFieldContentDidChange:(NSNotification *)notification
  99. {
  100. NSLog(@"textFieldContentDidChange");
  101. if(_searchField.text.length)
  102. {
  103. _placeholderLabel.hidden = YES;
  104. }
  105. else
  106. {
  107. _placeholderLabel.hidden = NO;
  108. }
  109. }
  110. -(void)layoutSubviews
  111. {
  112. CGFloat containerW = self.frame.size.width;
  113. CGFloat containerH = self.frame.size.height;
  114. _containerView.frame = CGRectMake(0, 0, containerW, containerH);
  115. _containerView.backgroundColor = [UIColor clearColor];
  116. _containerView.layer.cornerRadius = containerH / 2.0;
  117. _containerView.layer.masksToBounds = YES;
  118. [self setRadius:containerH/2.0 corners:UIRectCornerAllCorners];
  119. _searchField.frame = CGRectMake(16.0 + 14.0, 0, containerW - (16.0 + 14.0) - 16.0, containerH);
  120. _searchField.font = [UIFont systemFontOfSize:14.0];
  121. _searchField.returnKeyType = UIReturnKeySearch;
  122. //绘制圆角 要设置的圆角 使用“|”来组合
  123. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_searchField.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(30.0 / 2.0, 30.0 / 2.0)];
  124. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  125. //设置大小
  126. maskLayer.frame = _searchField.bounds;
  127. //设置图形样子
  128. maskLayer.path = maskPath.CGPath;
  129. _searchField.layer.mask = maskLayer;
  130. NSString *placeHolderStr = _placeholderStr.length?_placeholderStr:@"请输入关键词";
  131. CGSize needSize = [placeHolderStr boundingRectWithSize:CGSizeMake(MAXFLOAT, containerH) options:NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]} context:nil].size;
  132. //计算放大镜和提示语所占据宽度
  133. CGFloat needW = needSize.width + 2.0 + 4.0 + 14.0;
  134. _placeholderView.frame = CGRectMake((containerW - needW) / 2.0, 0, needW, 30.0);
  135. _searchX = (containerW - needW) / 2.0;
  136. _seachIconIV.frame = CGRectMake(0, (30.0 - 11.0) / 2.0, 14.0, 11.0);
  137. _seachIconIV.image = [UIImage imageNamed:@"chatmsg_search_icon"];
  138. _seachIconIV.userInteractionEnabled = NO;
  139. [_seachIconIV sizeToFit];
  140. _placeholderLabel.frame = CGRectMake(_seachIconIV.frame.origin.x + _seachIconIV.frame.size.width + 4.0, 0, needSize.width + 2.0, containerH);
  141. _placeholderLabel.text = placeHolderStr;
  142. _placeholderLabel.font = [UIFont systemFontOfSize:14.0];
  143. _placeholderLabel.userInteractionEnabled = YES;
  144. if(self.placeholderColor){
  145. _placeholderLabel.textColor = self.placeholderColor;
  146. }else{
  147. _placeholderLabel.textColor = [UIColor colorWithRed:(CGFloat)179.0 / 255.0 green:(CGFloat)179.0 / 255.0 blue:(CGFloat)179.0 / 255.0 alpha:1.0];
  148. }
  149. if(self.searchColor){
  150. _searchField.textColor = self.searchColor;
  151. }
  152. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToShowKeyboard:)];
  153. [_placeholderView addGestureRecognizer:tapGesture];
  154. }
  155. -(void)tapToShowKeyboard:(UITapGestureRecognizer *)tapGesture
  156. {
  157. [_searchField becomeFirstResponder];
  158. }
  159. #pragma mark UITextFieldDelegate
  160. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  161. {
  162. [self endEditing:YES];
  163. if(_delegate && [_delegate respondsToSelector:@selector(getSearchKeyWord:)])
  164. {
  165. [_delegate getSearchKeyWord:_searchField.text];
  166. }
  167. return YES;
  168. }
  169. @end