TextInputView.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. @interface TextInputView () <UITextViewDelegate>
  12. @property (nonatomic, strong) UIView *bottomView;
  13. @property (nonatomic, strong) NewTopicTextView *textView;
  14. @property (nonatomic, strong) UILabel *tipLabel;
  15. @property (nonatomic, strong) UILabel *numLabel;
  16. @property (nonatomic, strong) TDButton *enterButton;
  17. @end
  18. @implementation TextInputView
  19. - (void)dealloc
  20. {
  21. [_Notif removeObserver:self name:UIKeyboardDidShowNotification object:nil];
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. [self addSubview:self.bottomView];
  28. [self.bottomView addSubview:self.textView];
  29. [self.bottomView addSubview:self.tipLabel];
  30. [self.bottomView addSubview:self.numLabel];
  31. [self.bottomView addSubview:self.enterButton];
  32. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  33. [self addGestureRecognizer:tap];
  34. [_Notif addObserver:self selector:@selector(keyboardUp:) name:UIKeyboardDidShowNotification object:nil];
  35. [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.top.mas_equalTo(10);
  37. make.left.mas_equalTo(12);
  38. make.right.mas_equalTo(-12);
  39. make.height.mas_equalTo(95);
  40. }];
  41. self.hidden = YES;
  42. }
  43. return self;
  44. }
  45. - (void)setViewText:(NSString *)text
  46. {
  47. [self.textView setText:text];
  48. }
  49. - (void)setPlaceText:(NSString *)placeText;
  50. {
  51. [self.textView setPlaceholder:placeText];
  52. }
  53. - (void)startEditing {
  54. self.hidden = NO;
  55. [self.textView becomeFirstResponder];
  56. self.numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)self.textView.text.length];
  57. }
  58. - (void)dismiss
  59. {
  60. [self.textView resignFirstResponder];
  61. self.hidden = YES;
  62. }
  63. - (void)enterButtonAction:(TDButton *)sender {
  64. if (_textView.text.length < 1) {
  65. SHOWERROR(@"请先输入内容")
  66. return;
  67. }
  68. if (self.inputBlock) {
  69. self.inputBlock(_textView.text);
  70. _textView.text = @"";
  71. [self dismiss];
  72. }
  73. }
  74. - (void)startAnimationWithY:(CGFloat)y {
  75. self.bottomView.y = y;
  76. }
  77. #pragma mark - keyboardNotif
  78. - (void)keyboardUp:(NSNotification *)notification {
  79. // CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  80. // self.bottomView.transform = CGAffineTransformMakeTranslation(0,-30);
  81. }
  82. #pragma mark - UITextViewDelegate
  83. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  84. {
  85. return [XLTextCalculateHelper textView:textView shouldChangeTextInRange:range replacementText:text textLimit:1000];
  86. }
  87. - (void)textViewDidChange:(UITextView *)textView
  88. {
  89. BOOL willContinue = [XLTextCalculateHelper textViewDidChangeWillContinue:textView textLimit:1000];
  90. if (willContinue) {
  91. _numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)textView.text.length];
  92. }
  93. }
  94. #pragma mark - setter
  95. - (UIView *)bottomView {
  96. if (!_bottomView) {
  97. _bottomView = [UIView new];
  98. //原代码kGXScreenHeigh-150
  99. _bottomView.frame = CGRectMake(0, kGXScreenHeigh, kGXScreenWidth, 150.f);
  100. _bottomView.backgroundColor = UIColorHex(F5F5F5);
  101. }
  102. return _bottomView;
  103. }
  104. - (NewTopicTextView *)textView {
  105. if (!_textView) {
  106. _textView = [[NewTopicTextView alloc] init];
  107. _textView.delegate = self;
  108. _textView.frame = CGRectMake(12, 10, kGXScreenWidth-12-12, 95.f);
  109. _textView.backgroundColor = [UIColor whiteColor];
  110. _textView.layer.cornerRadius = 5.f;
  111. _textView.font = [UIFont systemFontOfSize:16];
  112. _textView.layer.borderWidth = 1.f;
  113. _textView.layer.borderColor = UIColorHex(#E6E6E6).CGColor;
  114. }
  115. return _textView;
  116. }
  117. - (UILabel *)tipLabel {
  118. if (!_tipLabel) {
  119. _tipLabel = [UILabel new];
  120. _tipLabel.text = @"支持中英文/数字";
  121. _tipLabel.textColor = UIColorHex(666666);
  122. _tipLabel.font = [UIFont systemFontOfSize:14.f];
  123. CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero];
  124. _tipLabel.frame = CGRectMake(14, 10+95+6, tipSize.width, tipSize.height);
  125. }
  126. return _tipLabel;
  127. }
  128. - (UILabel *)numLabel {
  129. if (!_numLabel) {
  130. _numLabel = [UILabel new];
  131. _numLabel.textAlignment = NSTextAlignmentRight;
  132. _numLabel.text = @"0/1000";
  133. _numLabel.textColor = UIColorHex(666666);
  134. _numLabel.font = [UIFont systemFontOfSize:14.f];
  135. CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero];
  136. _numLabel.frame = CGRectMake(30, 10+95-2-tipSize.height, kGXScreenWidth-30-30, tipSize.height);
  137. }
  138. return _numLabel;
  139. }
  140. - (TDButton *)enterButton {
  141. if (!_enterButton) {
  142. _enterButton = [[TDButton alloc] init];
  143. _enterButton.frame = CGRectMake(kGXScreenWidth-15-65, 10+95+6, 65, 28);
  144. [_enterButton setTitle:@"发送" forState:UIControlStateNormal];
  145. [_enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  146. [[_enterButton titleLabel] setFont:[UIFont systemFontOfSize:14.f]];
  147. _enterButton.backgroundColor = UIColorHex(1682DA);
  148. _enterButton.layer.cornerRadius = 2.f;
  149. [_enterButton addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  150. }
  151. return _enterButton;
  152. }
  153. @end