TextInputView.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // TextInputView.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/27.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "TextInputView.h"
  9. #import "XLTextCalculateHelper.h"
  10. #import "NewTopicTextView.h"
  11. #define STARTAG 100
  12. @interface TextInputView () <UITextViewDelegate>
  13. @property (nonatomic, strong) UIView *bottomView;
  14. @property (nonatomic, strong) UIView *starView;
  15. @property (nonatomic, strong) NewTopicTextView *textView;
  16. @property (nonatomic, strong) UILabel *tipLabel;
  17. @property (nonatomic, strong) UILabel *numLabel;
  18. @property (nonatomic, strong) TDButton *enterButton;
  19. @property (nonatomic, strong) UIButton *disBtn;
  20. @end
  21. @implementation TextInputView
  22. //- (void)dealloc
  23. //{
  24. // [_Notif removeObserver:self name:UIKeyboardDidShowNotification object:nil];
  25. //}
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. [self addSubview:self.bottomView];
  31. [self addSubview:self.starView];
  32. // [self addSubview:self.disBtn];
  33. [self.bottomView addSubview:self.textView];
  34. [self.bottomView addSubview:self.tipLabel];
  35. [self.bottomView addSubview:self.numLabel];
  36. [self.bottomView addSubview:self.enterButton];
  37. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.left.right.mas_equalTo(self);
  39. make.bottom.mas_equalTo(self.mas_safeAreaLayoutGuideBottom);
  40. make.height.mas_offset(150);
  41. }];
  42. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  43. [self addGestureRecognizer:tap];
  44. // [_Notif addObserver:self selector:@selector(keyboardUp:) name:UIKeyboardDidShowNotification object:nil];
  45. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.top.mas_offset(10);
  47. make.left.mas_offset(12);
  48. make.right.mas_offset(-12);
  49. make.height.mas_offset(95);
  50. }];
  51. [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.bottom.mas_equalTo(self.bottomView.mas_top);
  53. make.right.left.mas_equalTo(self);
  54. make.height.mas_offset(0);
  55. }];
  56. // [self.disBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  57. // make.bottom.mas_equalTo(self.starView.mas_top);
  58. // make.right.top.left.mas_equalTo(self);
  59. // }];
  60. self.starView.hidden = YES;
  61. self.hidden = YES;
  62. }
  63. return self;
  64. }
  65. - (void)setStar:(BOOL)star
  66. {
  67. _star = star;
  68. self.starView.hidden = NO;
  69. [self addStarView];
  70. [self.starView mas_updateConstraints:^(MASConstraintMaker *make) {
  71. make.height.mas_offset(46);
  72. }];
  73. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
  74. }];
  75. [self.starView addGestureRecognizer:tap];
  76. }
  77. - (void)addStarView
  78. {
  79. WS(weakSelf);
  80. UILabel * label = [UILabel new];
  81. label.text = @"评分:";
  82. label.textColor = UIColorHex(0x0B0B0B);
  83. label.font = [UIFont systemFontOfSize:16];
  84. [self.starView addSubview:label];
  85. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.left.mas_offset(15);
  87. make.centerY.mas_equalTo(self.starView);
  88. }];
  89. for (NSInteger i = 0; i < 5; i ++) {
  90. UIImageView * imageV = [UIImageView new];
  91. [self.starView addSubview:imageV];
  92. imageV.image = IMG(@"comment_star_no");
  93. imageV.tag = STARTAG + i;
  94. [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.left.mas_equalTo(label.mas_right).offset(20 + i * 30);
  96. make.centerY.mas_equalTo(self.starView);
  97. make.size.mas_offset(CGSizeMake(21, 20));
  98. }];
  99. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
  100. if (weakSelf.StarBlock) {
  101. weakSelf.StarBlock(imageV.tag - STARTAG);
  102. }
  103. dispatch_async(dispatch_get_main_queue(), ^{
  104. [weakSelf changeStarValue:imageV.tag - STARTAG];
  105. });
  106. }];
  107. imageV.userInteractionEnabled = YES;
  108. [imageV addGestureRecognizer:tap];
  109. }
  110. // UIView * lineV = [UIView new];
  111. // lineV.backgroundColor = LINEBGCOLOR;
  112. // [self.starView addSubview:lineV];
  113. }
  114. - (void)changeStarValue:(NSInteger)index
  115. {
  116. [self resetStarView];
  117. for (NSInteger i = 0; i <= index; i ++) {
  118. UIImageView * imageV = [self.starView viewWithTag:STARTAG + i];
  119. imageV.image = IMG(@"comment_star_yes");
  120. }
  121. }
  122. #pragma mark - 重置星星
  123. - (void)resetStarView
  124. {
  125. for (NSInteger i = 0; i < 5; i ++) {
  126. UIImageView * imageV = [self.starView viewWithTag:STARTAG + i];
  127. imageV.image = IMG(@"comment_star_no");
  128. }
  129. }
  130. - (void)setViewText:(NSString *)text
  131. {
  132. [self.textView setText:text];
  133. }
  134. - (void)setPlaceText:(NSString *)placeText;
  135. {
  136. [self.textView setPlaceholder:placeText];
  137. }
  138. - (void)startEditing {
  139. self.hidden = NO;
  140. [self.textView becomeFirstResponder];
  141. self.numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)self.textView.text.length];
  142. }
  143. - (void)dismiss
  144. {
  145. self.hidden = YES;
  146. [self.textView resignFirstResponder];
  147. }
  148. - (void)enterButtonAction:(TDButton *)sender {
  149. if (_textView.text.length < 1) {
  150. SHOWERROR(@"请先输入内容")
  151. return;
  152. }
  153. if (self.inputBlock) {
  154. self.inputBlock(_textView.text);
  155. _textView.text = @"";
  156. [self dismiss];
  157. }
  158. }
  159. - (void)startAnimationWithY:(CGFloat)y {
  160. self.bottomView.y = y;
  161. if (self.star) {
  162. self.starView.y = y - 46;
  163. }
  164. }
  165. #pragma mark - keyboardNotif
  166. //- (void)keyboardUp:(NSNotification *)notification {
  167. //// CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  168. //// self.bottomView.transform = CGAffineTransformMakeTranslation(0,-30);
  169. //}
  170. #pragma mark - UITextViewDelegate
  171. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  172. {
  173. return [XLTextCalculateHelper textView:textView shouldChangeTextInRange:range replacementText:text textLimit:1000];
  174. }
  175. - (void)textViewDidChange:(UITextView *)textView
  176. {
  177. BOOL willContinue = [XLTextCalculateHelper textViewDidChangeWillContinue:textView textLimit:1000];
  178. if (willContinue) {
  179. _numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)textView.text.length];
  180. }
  181. }
  182. #pragma mark - setter
  183. - (UIView *)bottomView {
  184. if (!_bottomView) {
  185. _bottomView = [UIView new];
  186. //原代码kGXScreenHeigh-150
  187. // _bottomView.frame = CGRectMake(0, kGXScreenHeigh-150, kGXScreenWidth, 150.f);
  188. _bottomView.backgroundColor = UIColorHex(F5F5F5);
  189. }
  190. return _bottomView;
  191. }
  192. - (UIView *)starView {
  193. if (!_starView) {
  194. _starView = [UIView new];
  195. _starView.backgroundColor = UIColorHex(F5F5F5);
  196. }
  197. return _starView;
  198. }
  199. - (NewTopicTextView *)textView {
  200. if (!_textView) {
  201. _textView = [[NewTopicTextView alloc] init];
  202. _textView.delegate = self;
  203. _textView.frame = CGRectMake(12, 10, kGXScreenWidth-12-12, 95.f);
  204. _textView.backgroundColor = [UIColor whiteColor];
  205. _textView.layer.cornerRadius = 5.f;
  206. _textView.font = [UIFont systemFontOfSize:16];
  207. _textView.layer.borderWidth = 1.f;
  208. _textView.layer.borderColor = UIColorHex(#E5E5E5).CGColor;
  209. }
  210. return _textView;
  211. }
  212. - (UILabel *)tipLabel {
  213. if (!_tipLabel) {
  214. _tipLabel = [UILabel new];
  215. _tipLabel.text = @"支持中英文/数字";
  216. _tipLabel.textColor = UIColorHex(666666);
  217. _tipLabel.font = [UIFont systemFontOfSize:14.f];
  218. CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero];
  219. _tipLabel.frame = CGRectMake(14, 10+95+6, tipSize.width, tipSize.height);
  220. }
  221. return _tipLabel;
  222. }
  223. - (UILabel *)numLabel {
  224. if (!_numLabel) {
  225. _numLabel = [UILabel new];
  226. _numLabel.textAlignment = NSTextAlignmentRight;
  227. _numLabel.text = @"0/1000";
  228. _numLabel.textColor = UIColorHex(666666);
  229. _numLabel.font = [UIFont systemFontOfSize:14.f];
  230. CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero];
  231. _numLabel.frame = CGRectMake(30, 10+95-2-tipSize.height, kGXScreenWidth-30-30, tipSize.height);
  232. }
  233. return _numLabel;
  234. }
  235. - (TDButton *)enterButton {
  236. if (!_enterButton) {
  237. _enterButton = [[TDButton alloc] init];
  238. _enterButton.frame = CGRectMake(kGXScreenWidth-15-65, 10+95+6, 65, 28);
  239. [_enterButton setTitle:@"发送" forState:UIControlStateNormal];
  240. [_enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  241. [[_enterButton titleLabel] setFont:[UIFont systemFontOfSize:14.f]];
  242. _enterButton.backgroundColor = UIColorHex(1682DA);
  243. _enterButton.layer.cornerRadius = 2.f;
  244. [_enterButton addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  245. }
  246. return _enterButton;
  247. }
  248. - (UIButton *)disBtn
  249. {
  250. if (!_disBtn) {
  251. _disBtn = [UIButton new];
  252. _disBtn.backgroundColor = [UIColor blackColor];
  253. _disBtn.alpha = 0.5;
  254. [_disBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  255. }
  256. return _disBtn;
  257. }
  258. - (void)dealloc
  259. {
  260. [self removeAllSubviews];
  261. }
  262. @end