6

默认情况下,UITextView 的 contentView 变为可滚动的,当文本过多时基于它的高度无法放入 textview 中。

我想禁用它,而是更新 UITextView 的高度以适合文本。我这样做的原因是因为我将 UITextView 添加为 UIScrollView 的子视图,它应该处理滚动,就像在本机邮件应用程序中一样(当您输入文本时,整个视图向上滚动,而不仅仅是 textview .

有人有一些想法/以前遇到过同样的问题吗?

4

2 回答 2

20

这样做非常简单:

CGRect frame = textView.frame;

frame.size = textView.contentSize;

textView.frame = frame;

这应该设置 textview 的高度以适当地适合其所有内容。

于 2010-04-06T12:59:00.700 回答
9

几个小改动:

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

    CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1;

    CGRect newTextFrame = textView.frame;
    newTextFrame.size = textView.contentSize;
    newTextFrame.size.height = newTextFrame.size.height + fontHeight;
    textView.frame = newTextFrame;
}

当您拼写错误时,添加字体高度可为自动更正框提供空间。

UITextView也应该设置为不滚动:

[aTextView setScrollEnabled:NO];
于 2010-04-06T15:25:34.090 回答