Autoexpand UITextView高度包含动态加载的完整文本

时间:2012-05-15 09:15:01

标签: ios uitextview

我有一个简单的textView动态填充数据。我想在填充数据后调整textview的高度,以便我看不到垂直滚动,也不会剪切文本。

是否可以通过选择其中一种textView模式来完成,还是我需要programaticaaly?

2 个答案:

答案 0 :(得分: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
} 

答案 1 :(得分:1)

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

sizeWithFont:
sizeWithFont:constrainedToSize:
sizeWithFont:constrainedToSize:lineBreakMode:
sizeWithFont:forWidth:lineBreakMode:
sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: