LZBKeyBoardToolBar.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // LZBKeyBoardToolBar.m
  3. // LZBKeyBoardView
  4. //
  5. // demo地址:https://github.com/lzbgithubcode/LZBKeyBoardView.git
  6. // Created by zibin on 16/12/4.
  7. // Copyright © 2016年 apple. All rights reserved.
  8. //
  9. #import "LZBKeyBoardToolBar.h"
  10. #import "LZBTextView.h"
  11. #import "UIView+LZBViewFrame.h"
  12. //颜色转换
  13. #define LZBColorRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
  14. #define LZBKeyboardBundleImage(name) [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",@"Resource.bundle/",name]]
  15. #define kKeyboardViewToolBarHeight 50 // 默认键盘输入工具条的高度
  16. #define kKeyboardViewToolBar_TextView_Height 35 // 默认键盘输入框的高度
  17. #define kKeyboardViewToolBar_TextView_LimitHeight 60 // 默认键盘输入框的限制高度
  18. #define kKeyboardViewToolBar_SendBtn_Width 40 // 默认发送按钮的宽度
  19. #define kKeyboardViewToolBar_Horizontal_DefaultMargin 15 //水平方向默认间距
  20. #define kKeyboardViewToolBar_Vertical_DefaultMargin 8 //垂直方向默认间距
  21. #define LZBScreenHeight [UIScreen mainScreen].bounds.size.height
  22. #define LZBScreenWidth [UIScreen mainScreen].bounds.size.width
  23. #define inputextViewFont [UIFont systemFontOfSize:14.0]
  24. @interface LZBKeyBoardToolBar()
  25. //View
  26. @property (nonatomic, strong) LZBTextView *inputTextView; //输入框
  27. @property (nonatomic, strong) UIView *topLine; // 顶部分割线
  28. @property (nonatomic, strong) UIView *bottomLine; // 底部分割线
  29. @property (nonatomic, strong) UIButton *sendBtn; // 发送按钮
  30. //data
  31. @property (nonatomic, copy) void(^sendTextBlock)(NSString *text); //输入框输入字符串回调Blcok
  32. @property (nonatomic, assign) CGFloat textHeight; //输入文字高度
  33. @property (nonatomic, assign) CGFloat animationDuration; //动画时间
  34. @property (nonatomic, strong) NSString *placeHolder; //占位文字
  35. @end
  36. @implementation LZBKeyBoardToolBar
  37. #pragma mark - API
  38. + (LZBKeyBoardToolBar *)showKeyBoardWithConfigToolBarHeight:(CGFloat)toolBarHeight sendTextCompletion:(void(^)(NSString *sendText))sendTextBlock
  39. {
  40. LZBKeyBoardToolBar *toolBar = [[LZBKeyBoardToolBar alloc]init];
  41. toolBar.backgroundColor = LZBColorRGB(247, 248, 250);
  42. if(toolBarHeight < kKeyboardViewToolBarHeight)
  43. toolBarHeight = kKeyboardViewToolBarHeight;
  44. toolBar.frame = CGRectMake(0, LZBScreenHeight - toolBarHeight, LZBScreenWidth, toolBarHeight);
  45. toolBar.sendTextBlock = sendTextBlock;
  46. return toolBar;
  47. }
  48. - (void)setInputViewPlaceHolderText:(NSString *)placeText
  49. {
  50. self.inputTextView.placeholder = placeText;
  51. self.placeHolder = placeText;
  52. }
  53. - (void)becomeFirstResponder{
  54. [self.inputTextView becomeFirstResponder];
  55. self.hidden = NO;
  56. }
  57. - (void)resignFirstResponder{
  58. [self.inputTextView resignFirstResponder];
  59. }
  60. #pragma mark - private
  61. - (instancetype)initWithFrame:(CGRect)frame
  62. {
  63. if(self = [super initWithFrame:frame])
  64. {
  65. [self youpaifsetupUI];
  66. }
  67. return self;
  68. }
  69. - (void)youpaifsetupUI
  70. {
  71. [self addSubview:self.inputTextView];
  72. [self addSubview:self.topLine];
  73. [self addSubview:self.bottomLine];
  74. [self addSubview:self.sendBtn];
  75. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  76. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self.inputTextView];
  77. }
  78. - (void)layoutSubviews
  79. {
  80. [super layoutSubviews];
  81. __weak typeof(self) weakSelf = self;
  82. CGFloat height = (self.textHeight + kKeyboardViewToolBar_TextView_Height)> kKeyboardViewToolBarHeight ? (self.textHeight + kKeyboardViewToolBar_TextView_Height) : kKeyboardViewToolBarHeight;
  83. CGFloat offsetY = self.LZB_heigth - height;
  84. [UIView animateWithDuration:self.animationDuration animations:^{
  85. weakSelf.LZB_y += offsetY;
  86. weakSelf.LZB_heigth = height;
  87. }];
  88. self.topLine.LZB_width = self.LZB_width;
  89. self.bottomLine.LZB_width = self.LZB_width;
  90. CGSize sendButtonSize = self.sendBtn.currentImage.size;
  91. self.sendBtn.LZB_width = sendButtonSize.width;
  92. self.sendBtn.LZB_heigth = sendButtonSize.height;
  93. self.sendBtn.LZB_x = self.LZB_width - sendButtonSize.width - kKeyboardViewToolBar_Horizontal_DefaultMargin;
  94. self.inputTextView.LZB_width = self.LZB_width - sendButtonSize.width - 3 *kKeyboardViewToolBar_Horizontal_DefaultMargin;
  95. self.inputTextView.LZB_x = kKeyboardViewToolBar_Horizontal_DefaultMargin;
  96. [UIView animateWithDuration:self.animationDuration animations:^{
  97. weakSelf.inputTextView.LZB_heigth = weakSelf.LZB_heigth - 2 *kKeyboardViewToolBar_Vertical_DefaultMargin;
  98. weakSelf.inputTextView.LZB_centerY = weakSelf.LZB_heigth * 0.5;
  99. weakSelf.sendBtn.LZB_y = weakSelf.LZB_heigth - sendButtonSize.height -kKeyboardViewToolBar_Vertical_DefaultMargin;
  100. weakSelf.bottomLine.LZB_y = weakSelf.LZB_heigth - weakSelf.bottomLine.LZB_heigth;
  101. }];
  102. [self.inputTextView setNeedsUpdateConstraints];
  103. }
  104. #pragma mark - handle
  105. - (void)keyboardWillChangeFrame:(NSNotification *)notification
  106. {
  107. CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  108. CGFloat keyboardHeight = keyboardFrame.size.height;
  109. CGFloat keyboardAnimaitonDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  110. self.animationDuration = keyboardAnimaitonDuration;
  111. NSInteger option = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
  112. //判断键盘是否出现
  113. BOOL isKeyBoardHidden = LZBScreenHeight == keyboardFrame.origin.y;
  114. CGFloat offsetMarginY = isKeyBoardHidden ? LZBScreenHeight - self.LZB_heigth :LZBScreenHeight - self.LZB_heigth - keyboardHeight;
  115. [UIView animateKeyframesWithDuration:self.animationDuration delay:0 options:option animations:^{
  116. self.LZB_y = offsetMarginY;
  117. } completion:nil];
  118. }
  119. - (void)textDidChange
  120. {
  121. if([self.inputTextView.text containsString:@"\n"])
  122. {
  123. [self sendBtnClick];
  124. return;
  125. }
  126. CGFloat margin = self.inputTextView.textContainerInset.left + self.inputTextView.textContainerInset.right;
  127. CGFloat height = [self.inputTextView.text boundingRectWithSize:CGSizeMake(self.inputTextView.LZB_width - margin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.inputTextView.font} context:nil].size.height;
  128. if(height == self.textHeight) return;
  129. // 确保输入框不会无限增高,控制在显示4行
  130. if (height > kKeyboardViewToolBar_TextView_LimitHeight) {
  131. return;
  132. }
  133. self.textHeight = height;
  134. [self setNeedsLayout];
  135. }
  136. - (void)sendBtnClick
  137. {
  138. if(self.sendTextBlock)
  139. self.sendTextBlock(self.inputTextView.text);
  140. self.textHeight = 0;
  141. [self resetInputView];
  142. }
  143. - (void)resetInputView
  144. {
  145. self.inputTextView.text = @"";
  146. [self setInputViewPlaceHolderText:self.placeHolder.length > 0 ? self.placeHolder : @""];
  147. [self.inputTextView resignFirstResponder];
  148. self.inputTextView.placeHolderHidden = self.inputTextView.hasText;
  149. }
  150. #pragma mark - lazy
  151. - (UIButton *)sendBtn
  152. {
  153. if(_sendBtn == nil)
  154. {
  155. _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  156. [_sendBtn setImage:LZBKeyboardBundleImage(@"btn_comment_expression_send") forState:UIControlStateNormal];
  157. [_sendBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  158. [_sendBtn addTarget:self action:@selector(sendBtnClick) forControlEvents:UIControlEventTouchUpInside];
  159. }
  160. return _sendBtn;
  161. }
  162. - (LZBTextView *)inputTextView
  163. {
  164. if(_inputTextView == nil)
  165. {
  166. _inputTextView = [[LZBTextView alloc]init];
  167. _inputTextView.layer.cornerRadius = 4;
  168. _inputTextView.layer.masksToBounds = YES;
  169. _inputTextView.layer.borderWidth = 1;
  170. _inputTextView.layer.borderColor = LZBColorRGB(221, 221, 221).CGColor;
  171. _inputTextView.font = inputextViewFont;
  172. _inputTextView.textColor = LZBColorRGB(102, 102, 102);
  173. _inputTextView.tintColor = _inputTextView.textColor;
  174. _inputTextView.enablesReturnKeyAutomatically = YES;
  175. _inputTextView.returnKeyType = UIReturnKeySend;
  176. _inputTextView.placeholderColor = LZBColorRGB(150, 150, 150);
  177. }
  178. return _inputTextView;
  179. }
  180. - (UIView *)topLine{
  181. if (_topLine == nil) {
  182. _topLine = [[UIView alloc] init];
  183. _topLine.LZB_heigth = 0.5;
  184. _topLine.backgroundColor = LZBColorRGB(214, 214, 214);
  185. }
  186. return _topLine;
  187. }
  188. - (UIView *)bottomLine{
  189. if (_bottomLine == nil) {
  190. _bottomLine = [[UIView alloc] init];
  191. _bottomLine.backgroundColor = LZBColorRGB(214, 214, 214);
  192. _bottomLine.LZB_heigth = 0.5;
  193. _bottomLine.hidden = YES;
  194. }
  195. return _bottomLine;
  196. }
  197. @end