TextInputView.m 5.4 KB

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