// // TextInputView.m // TheoryNetwork // // Created by tederen on 2019/9/27. // Copyright © 2019 tederen. All rights reserved. // #import "TextInputView.h" #import "XLTextCalculateHelper.h" #import "NewTopicTextView.h" @interface TextInputView () @property (nonatomic, strong) UIView *bottomView; @property (nonatomic, strong) NewTopicTextView *textView; @property (nonatomic, strong) UILabel *tipLabel; @property (nonatomic, strong) UILabel *numLabel; @property (nonatomic, strong) TDButton *enterButton; @end @implementation TextInputView - (void)dealloc { [_Notif removeObserver:self name:UIKeyboardDidShowNotification object:nil]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self addSubview:self.bottomView]; [self.bottomView addSubview:self.textView]; [self.bottomView addSubview:self.tipLabel]; [self.bottomView addSubview:self.numLabel]; [self.bottomView addSubview:self.enterButton]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]; [self addGestureRecognizer:tap]; [_Notif addObserver:self selector:@selector(keyboardUp:) name:UIKeyboardDidShowNotification object:nil]; [self.textView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(10); make.left.mas_equalTo(12); make.right.mas_equalTo(-12); make.height.mas_equalTo(95); }]; self.hidden = YES; } return self; } - (void)setPlaceText:(NSString *)placeText; { [self.textView setPlaceholder:placeText]; } - (void)startEditing { self.hidden = NO; [self.textView becomeFirstResponder]; self.numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)self.textView.text.length]; } - (void)dismiss { [self.textView resignFirstResponder]; self.hidden = YES; } - (void)enterButtonAction:(TDButton *)sender { if (_textView.text.length < 1) { SHOWERROR(@"请先输入内容") return; } if (self.inputBlock) { self.inputBlock(_textView.text); _textView.text = @""; [self dismiss]; } } - (void)startAnimationWithY:(CGFloat)y { self.bottomView.y = y; } #pragma mark - keyboardNotif - (void)keyboardUp:(NSNotification *)notification { // CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; // self.bottomView.transform = CGAffineTransformMakeTranslation(0,-30); } #pragma mark - UITextViewDelegate - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return [XLTextCalculateHelper textView:textView shouldChangeTextInRange:range replacementText:text textLimit:1000]; } - (void)textViewDidChange:(UITextView *)textView { BOOL willContinue = [XLTextCalculateHelper textViewDidChangeWillContinue:textView textLimit:1000]; if (willContinue) { _numLabel.text = [NSString stringWithFormat:@"%lu/1000", (unsigned long)textView.text.length]; } } #pragma mark - setter - (UIView *)bottomView { if (!_bottomView) { _bottomView = [UIView new]; //原代码kGXScreenHeigh-150 _bottomView.frame = CGRectMake(0, kGXScreenHeigh, kGXScreenWidth, 150.f); _bottomView.backgroundColor = UIColorHex(F5F5F5); } return _bottomView; } - (NewTopicTextView *)textView { if (!_textView) { _textView = [[NewTopicTextView alloc] init]; _textView.delegate = self; _textView.frame = CGRectMake(12, 10, kGXScreenWidth-12-12, 95.f); _textView.backgroundColor = [UIColor whiteColor]; _textView.layer.cornerRadius = 5.f; _textView.font = [UIFont systemFontOfSize:16]; _textView.layer.borderWidth = 1.f; _textView.layer.borderColor = UIColorHex(#E6E6E6).CGColor; } return _textView; } - (UILabel *)tipLabel { if (!_tipLabel) { _tipLabel = [UILabel new]; _tipLabel.text = @"支持中英文/数字"; _tipLabel.textColor = UIColorHex(666666); _tipLabel.font = [UIFont systemFontOfSize:14.f]; CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero]; _tipLabel.frame = CGRectMake(14, 10+95+6, tipSize.width, tipSize.height); } return _tipLabel; } - (UILabel *)numLabel { if (!_numLabel) { _numLabel = [UILabel new]; _numLabel.textAlignment = NSTextAlignmentRight; _numLabel.text = @"0/1000"; _numLabel.textColor = UIColorHex(666666); _numLabel.font = [UIFont systemFontOfSize:14.f]; CGSize tipSize = [_tipLabel sizeThatFits:CGSizeZero]; _numLabel.frame = CGRectMake(30, 10+95-2-tipSize.height, kGXScreenWidth-30-30, tipSize.height); } return _numLabel; } - (TDButton *)enterButton { if (!_enterButton) { _enterButton = [[TDButton alloc] init]; _enterButton.frame = CGRectMake(kGXScreenWidth-15-65, 10+95+6, 65, 28); [_enterButton setTitle:@"发送" forState:UIControlStateNormal]; [_enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [[_enterButton titleLabel] setFont:[UIFont systemFontOfSize:14.f]]; _enterButton.backgroundColor = UIColorHex(1682DA); _enterButton.layer.cornerRadius = 2.f; [_enterButton addTarget:self action:@selector(enterButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _enterButton; } @end