2

我有一个简单的 textView,它的数据是动态填充的。一旦填充了数据,我想调整 textview 的高度,这样我就看不到垂直滚动,也看不到文本被剪裁。

可以简单地通过选择其中一种 textView 模式来完成,还是我需要通过编程来完成?

4

2 回答 2

7

您可以通过将这几行代码添加到您的应用程序来做到这一点:-

CGRect frame = textView.frame; 
frame.size.height = textView.contentSize.height; 
textView.frame = frame; 

在 textview 的委托方法中实现此代码

- (void)textViewDidChange:(UITextView *)textView 
{
// you might have to compare the frames if it needs changing as user might not have added an extra line
} 
于 2012-05-15T09:25:09.457 回答
1

您必须以编程方式进行。使用NSString UIKit Additions计算 UITextView 的高度:

sizeWithFont:
sizeWithFont:constrainedToSize:
sizeWithFont:constrainedToSize:lineBreakMode:
sizeWithFont:forWidth:lineBreakMode:
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
于 2012-05-15T09:22:48.347 回答