// // 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" #define STARTAG 100 @interface TextInputView () @property (nonatomic, strong) UIView *bottomView; @property (nonatomic, strong) UIView *starView; @property (nonatomic, strong) NewTopicTextView *textView; @property (nonatomic, strong) UILabel *tipLabel; @property (nonatomic, strong) UILabel *numLabel; @property (nonatomic, strong) TDButton *enterButton; @property (nonatomic, strong) UIButton *disBtn; @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 addSubview:self.starView]; // [self addSubview:self.disBtn]; [self.bottomView addSubview:self.textView]; [self.bottomView addSubview:self.tipLabel]; [self.bottomView addSubview:self.numLabel]; [self.bottomView addSubview:self.enterButton]; [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self); make.bottom.mas_equalTo(self.mas_safeAreaLayoutGuideBottom); make.height.mas_offset(150); }]; 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_offset(10); make.left.mas_offset(12); make.right.mas_offset(-12); make.height.mas_offset(95); }]; [self.starView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.bottomView.mas_top); make.right.left.mas_equalTo(self); make.height.mas_offset(0); }]; // [self.disBtn mas_makeConstraints:^(MASConstraintMaker *make) { // make.bottom.mas_equalTo(self.starView.mas_top); // make.right.top.left.mas_equalTo(self); // }]; self.starView.hidden = YES; self.hidden = YES; } return self; } - (void)setStar:(BOOL)star { _star = star; self.starView.hidden = NO; [self addStarView]; [self.starView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_offset(46); }]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) { }]; [self.starView addGestureRecognizer:tap]; } - (void)addStarView { WS(weakSelf); UILabel * label = [UILabel new]; label.text = @"评分:"; label.textColor = UIColorHex(0x0B0B0B); label.font = [UIFont systemFontOfSize:16]; [self.starView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_offset(15); make.centerY.mas_equalTo(self.starView); }]; for (NSInteger i = 0; i < 5; i ++) { UIImageView * imageV = [UIImageView new]; [self.starView addSubview:imageV]; imageV.image = IMG(@"comment_star_no"); imageV.tag = STARTAG + i; [imageV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(label.mas_right).offset(20 + i * 30); make.centerY.mas_equalTo(self.starView); make.size.mas_offset(CGSizeMake(21, 20)); }]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) { if (weakSelf.StarBlock) { weakSelf.StarBlock(imageV.tag - STARTAG); } dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf changeStarValue:imageV.tag - STARTAG]; }); }]; imageV.userInteractionEnabled = YES; [imageV addGestureRecognizer:tap]; } // UIView * lineV = [UIView new]; // lineV.backgroundColor = LINEBGCOLOR; // [self.starView addSubview:lineV]; } - (void)changeStarValue:(NSInteger)index { [self resetStarView]; for (NSInteger i = 0; i <= index; i ++) { UIImageView * imageV = [self.starView viewWithTag:STARTAG + i]; imageV.image = IMG(@"comment_star_yes"); } } #pragma mark - 重置星星 - (void)resetStarView { for (NSInteger i = 0; i < 5; i ++) { UIImageView * imageV = [self.starView viewWithTag:STARTAG + i]; imageV.image = IMG(@"comment_star_no"); } } - (void)setViewText:(NSString *)text { [self.textView setText:text]; } - (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.hidden = YES; [self.textView resignFirstResponder]; } - (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; if (self.star) { self.starView.y = y - 46; } } #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-150, kGXScreenWidth, 150.f); _bottomView.backgroundColor = UIColorHex(F5F5F5); } return _bottomView; } - (UIView *)starView { if (!_starView) { _starView = [UIView new]; _starView.backgroundColor = UIColorHex(F5F5F5); } return _starView; } - (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(#E5E5E5).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; } - (UIButton *)disBtn { if (!_disBtn) { _disBtn = [UIButton new]; _disBtn.backgroundColor = [UIColor blackColor]; _disBtn.alpha = 0.5; [_disBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; } return _disBtn; } - (void)dealloc { [self removeAllSubviews]; } @end