// // YOUPAIBBNIMInputToolBar.m // VQU // // Created by Elaine on 2021/10/26. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIBBNIMInputToolBar.h" #import "YOUPAINIMGrowingTextView.h" #import "UIView+NIM.h" #import "UIImage+NIMKit.h" #import "NIMInputBarItemType.h" #define leftSpacing 15.0 @interface YOUPAIBBNIMInputToolBar() @property (nonatomic,copy) NSArray *youpaiptypes; @property (nonatomic,copy) NSDictionary *youpaipdict; @property (nonatomic,strong) YOUPAINIMGrowingTextView *youpaipinputTextView; @property (nonatomic,assign) BBNIMInputStatus youpaipstatus; @end @implementation YOUPAIBBNIMInputToolBar - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self setBackgroundColor:LCBkgColor]; _youpaipinputTextView = [[YOUPAINIMGrowingTextView alloc] initWithFrame:CGRectZero]; _youpaipinputTextView.font = [UIFont systemFontOfSize:14.0f]; _youpaipinputTextView.maxNumberOfLines = 4; _youpaipinputTextView.minNumberOfLines = 1; _youpaipinputTextView.textColor = ZYTextInputColor; _youpaipinputTextView.backgroundColor = HexColorFromRGB(0x31323D); _youpaipinputTextView.layer.cornerRadius = 8; _youpaipinputTextView.nim_size = [_youpaipinputTextView intrinsicContentSize]; _youpaipinputTextView.textViewDelegate = self; _youpaipinputTextView.returnKeyType = UIReturnKeySend; self.youpaiptypes = @[ @(NIMInputBarItemTypeVoice), @(NIMInputBarItemTypeTextAndRecord), @(NIMInputBarItemTypeEmoticon), @(NIMInputBarItemTypeMore), ]; //修改inputview 按钮 注释 by leo self.youpaiprecordBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaiprecordBtn setImage:[UIImage imageNamed:@"vqu_images_IM_im_record"] forState:UIControlStateNormal]; [self.youpaiprecordBtn setImage:[UIImage imageNamed:@"vqu_images_IM_record_select"] forState:UIControlStateSelected]; [self.youpaiprecordBtn sizeToFit]; self.youpaipcameraBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaipcameraBtn setImage:[UIImage imageNamed:@"vqu_images_IM_im_camera"] forState:UIControlStateNormal]; [self.youpaipcameraBtn sizeToFit]; self.youpaipphotoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaipphotoBtn setImage:[UIImage imageNamed:@"vqu_images_IM_photo"] forState:UIControlStateNormal]; [self.youpaipphotoBtn sizeToFit]; self.youpaipemoticonBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaipemoticonBtn setImage:[UIImage imageNamed:@"vqu_images_IM_emoji"] forState:UIControlStateNormal]; [self.youpaipemoticonBtn setImage:[UIImage imageNamed:@"vqu_images_vqu_images_IM_emoji_select"] forState:UIControlStateSelected]; [self.youpaipemoticonBtn sizeToFit]; self.youpaipgiftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaipgiftBtn setImage:[UIImage imageNamed:@"vqu_images_IM_gift"] forState:UIControlStateNormal]; [self.youpaipgiftBtn sizeToFit]; self.youpaipvideoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.youpaipvideoBtn setImage:[UIImage imageNamed:@"vqu_images_IM_video1"] forState:UIControlStateNormal]; [self.youpaipvideoBtn sizeToFit]; [self addSubview:_youpaipinputTextView]; [self addSubview:self.youpaiprecordBtn]; [self addSubview:self.youpaipcameraBtn]; [self addSubview:self.youpaipphotoBtn]; [self addSubview:self.youpaipemoticonBtn]; [self addSubview:self.youpaipgiftBtn]; [self addSubview:self.youpaipvideoBtn]; } return self; } - (void)setYoupaipinputBarItemTypes:(NSArray *)youpaiptypes{ self.youpaiptypes = youpaiptypes; [self setNeedsLayout]; } - (NSString *)contentText { return self.youpaipinputTextView.text; } - (void)setContentText:(NSString *)contentText { self.youpaipinputTextView.text = contentText; } - (NSArray *)youpaipinputBarItemTypes { return self.youpaiptypes; } - (CGSize)sizeThatFits:(CGSize)size { CGFloat viewHeight = 0.0f; if (self.youpaipstatus == BBNIMInputStatusAudio) { viewHeight = 70; }else{ //算出 TextView 的宽度 [self adjustTextViewWidth:size.width]; // TextView 自适应高度 [self.youpaipinputTextView layoutIfNeeded]; viewHeight = self.youpaipinputTextView.frame.size.height; //得到 ToolBar 自身高度 viewHeight = viewHeight +44+10; // viewHeight = 84; } return CGSizeMake(size.width,viewHeight); } - (void)adjustTextViewWidth:(CGFloat)width { // CGFloat textViewWidth = 0; // for (NSNumber *type in self.types) { // if (type.integerValue == NIMInputBarItemTypeTextAndRecord) { // continue; // } // UIView *view = [self subViewForType:type.integerValue]; // textViewWidth += view.nim_width; // } // textViewWidth += (self.spacing * (self.types.count + 1)); self.youpaipinputTextView.nim_width = self.nim_width-leftSpacing*2; } - (void)layoutSubviews{ [super layoutSubviews]; CGFloat bottomGap = -10; CGFloat size = 35; CGFloat midSpacing = (self.nim_width-leftSpacing*2-size*6)/5; [self.youpaiprecordBtn makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(leftSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; [self.youpaipcameraBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.youpaiprecordBtn.right).offset(midSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; [self.youpaipphotoBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.youpaipcameraBtn.right).offset(midSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; [self.youpaipemoticonBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.youpaipphotoBtn.right).offset(midSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; [self.youpaipgiftBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.youpaipemoticonBtn.right).offset(midSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; [self.youpaipvideoBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.youpaipgiftBtn.right).offset(midSpacing); make.bottom.mas_equalTo(bottomGap); make.size.mas_equalTo(CGSizeMake(size, size)); }]; // [self.youpaipvideoBtn makeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(self.youpaipemoticonBtn.right).offset(midSpacing); // make.bottom.mas_equalTo(bottomGap); // make.size.mas_equalTo(CGSizeMake(size, size)); // }]; [_youpaipinputTextView makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(leftSpacing); make.top.mas_equalTo(5); make.bottom.mas_equalTo(-(5+44)); make.right.mas_equalTo(-leftSpacing); }]; } - (void)youpaifadjustTextAndRecordView { // if ([self.types containsObject:@(NIMInputBarItemTypeTextAndRecord)]) // { // self.youpaipinputTextView.center = self.youpaipinputTextBkgImage.center; // // if (!self.youpaipinputTextView.superview) // { // //输入框 // [self addSubview:self.youpaipinputTextView]; // } // if (!self.youpaiprecordButton.superview) // { // //中间点击录音按钮 // self.youpaiprecordButton.frame = self.youpaipinputTextBkgImage.frame; // [self addSubview:self.youpaiprecordButton]; // } // } } - (BOOL)showsKeyboard { return [self.youpaipinputTextView isFirstResponder]; } - (void)setShowsKeyboard:(BOOL)showsKeyboard { if (showsKeyboard) { [self.youpaipinputTextView becomeFirstResponder]; } else { [self.youpaipinputTextView resignFirstResponder]; } } - (void)youpaifupdate:(BBNIMInputStatus)status { self.youpaipstatus = status; [self sizeToFit]; if (status == BBNIMInputStatusText || status == BBNIMInputStatusMore) { [self.youpaiprecordButton setHidden:YES]; [self.youpaipinputTextView setHidden:NO]; [self.youpaipinputTextBkgImage setHidden:NO]; [self updateVoiceBtnImages:YES]; [self updateEmotAndTextBtnImages:YES]; } else if(status == BBNIMInputStatusAudio) { [self.youpaiprecordButton setHidden:NO]; [self.youpaipinputTextView setHidden:YES]; [self.youpaipinputTextBkgImage setHidden:YES]; [self updateVoiceBtnImages:NO]; [self updateEmotAndTextBtnImages:YES]; } else { [self.youpaiprecordButton setHidden:YES]; [self.youpaipinputTextView setHidden:NO]; [self.youpaipinputTextBkgImage setHidden:NO]; [self updateVoiceBtnImages:YES]; [self updateEmotAndTextBtnImages:NO]; } } - (void)updateVoiceBtnImages:(BOOL)selected { // [self.youpaipvoiceBtn setImage:selected?[UIImage imageNamed:@"vqu_images_IM_voice"]:[UIImage imageNamed:@"im_keyboard"] forState:UIControlStateNormal]; // [self.youpaipvoiceBtn setImage:selected?[UIImage imageNamed:@"vqu_images_IM_voice"]:[UIImage imageNamed:@"im_keyboard"] forState:UIControlStateHighlighted]; } - (void)updateEmotAndTextBtnImages:(BOOL)selected { // [self.youpaipemoticonBtn setImage:selected?[UIImage imageNamed:@"vqu_images_IM_emoji"]:[UIImage imageNamed:@"im_keyboard"] forState:UIControlStateNormal]; // [self.youpaipemoticonBtn setImage:selected?[UIImage imageNamed:@"vqu_images_IM_emoji"]:[UIImage imageNamed:@"im_keyboard"] forState:UIControlStateHighlighted]; } #pragma mark - VQ_NIMGrowingTextViewDelegate - (BOOL)youpaifshouldChangeTextInRange:(NSRange)range replacementText:(NSString *)replacementText { BOOL youpaipshould = YES; if ([self.delegate respondsToSelector:@selector(shouldChangeTextInRange:replacementText:)]) { youpaipshould = [self.delegate shouldChangeTextInRange:range replacementText:replacementText]; } return youpaipshould; } - (BOOL)textViewShouldBeginEditing:(YOUPAINIMGrowingTextView *)growingTextView { BOOL youpaipshould = YES; if ([self.delegate respondsToSelector:@selector(textViewShouldBeginEditing)]) { youpaipshould = [self.delegate textViewShouldBeginEditing]; } return youpaipshould; } - (void)textViewDidEndEditing:(YOUPAINIMGrowingTextView *)growingTextView { if ([self.delegate respondsToSelector:@selector(textViewDidEndEditing)]) { [self.delegate textViewDidEndEditing]; } } - (void)textViewDidChange:(YOUPAINIMGrowingTextView *)growingTextView { if ([self.delegate respondsToSelector:@selector(textViewDidChange)]) { [self.delegate textViewDidChange]; } } - (void)willChangeHeight:(CGFloat)height { CGFloat toolBarHeight = height + 10+44; if ([self.delegate respondsToSelector:@selector(toolBarWillChangeHeight:)]) { [self.delegate toolBarWillChangeHeight:toolBarHeight]; } } - (void)didChangeHeight:(CGFloat)height { self.nim_height = height + 10+44; if ([self.delegate respondsToSelector:@selector(toolBarDidChangeHeight:)]) { [self.delegate toolBarDidChangeHeight:self.nim_height]; } } #pragma mark - Get - (UIView *)subViewForType:(NIMInputBarItemType)type{ if (!_youpaipdict) { _youpaipdict = @{ @(NIMInputBarItemTypeVoice) : self.youpaipvoiceBtn, @(NIMInputBarItemTypeTextAndRecord) : self.youpaipinputTextBkgImage, @(NIMInputBarItemTypeEmoticon) : self.youpaipemoticonBtn, @(NIMInputBarItemTypeMore) : self.youpaipmoreMediaBtn }; } return _youpaipdict[@(type)]; } - (CGFloat)spacing{ return 0.f; } - (CGFloat)textViewPadding { return 0.f; } @end @implementation YOUPAIBBNIMInputToolBar(InputText) - (NSRange)youpaifselectedRange { return self.youpaipinputTextView.selectedRange; } - (void)youpaifsetPlaceHolder:(NSString *)placeHolder { NSString* str ; if(placeHolder){ str = placeHolder; }else{ str = @""; } self.youpaipinputTextView.placeholderAttributedText = [[NSAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:14]}]; } - (void)youpaifinsertText:(NSString *)text { NSRange range = self.youpaipinputTextView.selectedRange; NSString *replaceText = [self.youpaipinputTextView.text stringByReplacingCharactersInRange:range withString:text]; range = NSMakeRange(range.location + text.length, 0); self.youpaipinputTextView.text = replaceText; self.youpaipinputTextView.selectedRange = range; } - (void)youpaifdeleteText:(NSRange)range { NSString *text = self.contentText; if (range.location + range.length <= [text length] && range.location != NSNotFound && range.length != 0) { NSString *newText = [text stringByReplacingCharactersInRange:range withString:@""]; NSRange newSelectRange = NSMakeRange(range.location, 0); [self.youpaipinputTextView setText:newText]; self.youpaipinputTextView.selectedRange = newSelectRange; } } @end