UITextView 评论输入框 高度自适应

来源:互联网 发布:中国8月金融数据 编辑:程序博客网 时间:2024/06/05 18:40



创建一个inputView继承于UIView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColorcolorWithRed:0.13green:0.13blue:0.13alpha:0.30];

        //使用NSNotificationCenter鍵盤出現時

        [[NSNotificationCenterdefaultCenter]addObserver:self

         

                                                 selector:@selector(keyboardWillShown:)

         

                                                     name:UIKeyboardWillShowNotificationobject:nil];

        

        //使用NSNotificationCenter鍵盤隐藏時

        [[NSNotificationCenterdefaultCenter]addObserver:self

         

                                                 selector:@selector(keyboardWillBeHidden:)

         

                                                     name:UIKeyboardWillHideNotificationobject:nil];

        [selfcreateView];

        //  点击黑色view 回收键盘

        UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction)];

        tap.delegate =self;

        self.userInteractionEnabled =YES;

        [selfaddGestureRecognizer:tap];

    }

    returnself;

}

- (void)createView{

// 输入框view

    self.inputView = [[UIViewalloc]initWithFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width,50)];

    [selfaddSubview:self.inputView];

    self.inputView.backgroundColor = [UIColorcolorWithRed:0.80green:0.82blue:0.84alpha:1.00];

    // 输入框

    self.inputTextView = [[UITextViewalloc]initWithFrame:CGRectMake(10,10, [UIScreenmainScreen].bounds.size.width-20,30)];

    self.inputTextView.delegate = self;

    self.inputTextView.returnKeyType = UIReturnKeySend;

    self.inputTextView.layer.cornerRadius = 4;

    self.inputTextView.layer.masksToBounds = YES;

    self.inputTextView.font = [UIFont systemFontOfSize:12];

    self.inputTextView.backgroundColor = [UIColorcolorWithRed:0.95green:0.95blue:0.95alpha:1.00];

    self.inputTextView.textColor = [UIColorblackColor];

    [self.inputViewaddSubview:self.inputTextView];

    // 占位文字

    self.placeLabel = [[UILabelalloc]initWithFrame:CGRectMake(6,10,200, 10)];

    self.placeLabel.text = @"最多可输入300";

    self.placeLabel.textColor = [UIColorlightGrayColor];

    [self.inputTextViewaddSubview:self.placeLabel];

    //self.placeLabel.backgroundColor = [UIColor yellowColor];

    self.placeLabel.font = [UIFont systemFontOfSize:12];

    self.placeLabel.userInteractionEnabled = NO;

    self.placeLabel.hidden =NO;

}

//这个函数的最后一个参数text代表你每次输入的的那个字,所以:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

   // NSLog(@"-----%@",text);

    if ([textisEqualToString:@""]) {

        //表示删除字符

    }

    // 如果点击的是发送按钮,,进行发送操作

    if ([textisEqualToString:@"\n"]){

        if (self.inputTextView.text.length>0) {

            [self.delegatesendText:self.inputTextView.text];

        }

        returnNO;

    }else{

     // 正常输入,计算输入字符长度限制在300字

        NSString *new = [textView.textstringByReplacingCharactersInRange:rangewithString:text];

        if(new.length >300){

            if (![textisEqualToString:@""]) {

                returnNO;

            }

        }

        returnYES;

    }

}


- (void)textViewDidChange:(UITextView *)textView{

    if (textView.text.length ==0) {

         // 只要输入的内容是空  就显示占位字符

        self.placeLabel.hidden =NO;

    }else{

        self.placeLabel.hidden =YES;

    }

     // 规定输入框最大高度。

    staticCGFloat maxHeight =120.0f;

    CGRect frame = textView.frame;

     // 计算文字高度

    CGSize constraintSize =CGSizeMake(frame.size.width,MAXFLOAT);

    CGSize size = [textViewsizeThatFits:constraintSize];

    if (size.height >= maxHeight){

        size.height = maxHeight;

        textView.scrollEnabled =YES;  // 大于最大高度允许滚动

    }else{

        textView.scrollEnabled =NO;   // 小于最大高度不允许滚动

    }

    [UIViewanimateWithDuration:0.2animations:^{

        // 动态的改变输入View的尺寸

        self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height-self.keyboldH-size.height-20, [UIScreen mainScreen].bounds.size.width, size.height+20);

        // 动态的改变输入框的尺寸

        textView.frame =CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);

    }];

}


//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置

- (void)keyboardWillShown:(NSNotification*)aNotification{

    NSDictionary* info = [aNotificationuserInfo];

    //kbSize即為鍵盤尺寸 (width, height)

    CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;//得到鍵盤的高度

    self.keyboldH = kbSize.height;

    CGFloat height =0;

    // 切换键盘的时候重新计算文字高度 设置输入view和输入框尺寸

    if (self.inputTextView.text == nil) {

        height = 50;

    }else{

        CGSize constraintSize =CGSizeMake([UIScreenmainScreen].bounds.size.width-20,MAXFLOAT);

        CGSize size = [self.inputTextViewsizeThatFits:constraintSize];

        height = size.height;

    }

    [UIViewanimateWithDuration:0.1animations:^{

        if (height>=120.0f) {

            self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height - kbSize.height-120.0f-20, [UIScreenmainScreen].bounds.size.width,120.0f+20);

        }else{

            self.inputView.frame =CGRectMake(0, [UIScreenmainScreen].bounds.size.height - kbSize.height-height-20, [UIScreenmainScreen].bounds.size.width, height+20);

        }

    }];

}


//当键盘隐藏的时候

- (void)keyboardWillBeHidden:(NSNotification*)aNotification{

    [UIViewanimateWithDuration:1animations:^{

        self.inputView.frame = CGRectMake(0, [UIScreenmainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width,50);

    }];

}



- (void)inputViewShow{

    [self.inputTextViewbecomeFirstResponder];

    

}

- (void)inputViewHiden{

    [self.inputTextViewresignFirstResponder];


}

- (void)tapAction{

    [selfremoveFromSuperview];

    [selfinputViewHiden];

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    if (touch.view ==self.inputView ) {

        returnNO;

    }else{

        returnYES;

    }

}



0 0