首页 文章

UITextView自动高度 - 向上方向

提问于
浏览
1

我正在使用此代码来生成具有自动调整大小的动态UITextView

- (void)textViewDidChange:(UITextView *)txtView
{
    CGRect frame;
    frame = txtView.frame;
    frame.size.height = [textView contentSize].height;
    textView.frame = frame;
}

代码很好,但是当我按下键盘上的“返回”(断行)时,我需要textView向上增长方向,而不是像现在这样向下 .

我尝试用“CGRectGetMaxY(textView.frame)”做一些事情,因为我知道textView的底部需要保持原样而不是改变,但我找不到正确的方法 .

这个textView用于聊天消息的输出,所以如果用户写的东西很长,那么textView需要长大,因为键盘是波纹管

1 回答

  • 5

    这应该做的伎俩:

    - (void)textViewDidChange:(UITextView *)textView
    {
        CGRect frame = textView.frame;
        frame.origin.y = CGRectGetMaxY(frame) - [textView contentSize].height;
        frame.size.height = [textView contentSize].height;
        textView.frame = frame;
    }
    

相关问题