美文网首页
输入框TextView自适应高度

输入框TextView自适应高度

作者: 汪小喵 | 来源:发表于2017-08-02 17:08 被阅读186次

自适应高度的输入框,这里直接上代码
在textView的代理方法中写入下面的代码就可以实现了

-(void)textViewDidChange:(UITextView *)textView{
    static CGFloat maxHeight =60.0f;
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    if (size.height > frame.size.height) {
        if (size.height >= maxHeight)
        {
            size.height = maxHeight;
            textView.scrollEnabled = YES;   // 允许滚动
        }
        else
        {
            textView.scrollEnabled = NO;    // 不允许滚动
        }
    textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}

相关文章

网友评论

      本文标题:输入框TextView自适应高度

      本文链接:https://www.haomeiwen.com/subject/yzlylxtx.html