0

我正在显示来自 sqlite3 数据库的一些文本,并希望 UITextView 根据文本量自动调整大小。我可以做到这一点,如下所示:

TextLabel.scrollEnabled = YES;
TextLabel.userInteractionEnabled = YES;

[TextLabel setFrame:CGRectMake(55.10, 26.15, TextLabel.contentSize.width, 
                                             TextLabel.contentSize.height)];

这工作正常。尝试设置最大高度时我很挣扎。

所以 UITextView 将根据文本调整大小,除非它达到最大高度,在这种情况下,用户将不得不使用滚动条来查看剩余的文本。

任何帮助将不胜感激

4

1 回答 1

1

这是一种发现某些多行文本所需高度的方法:

int myWidth = 300;
int maxHeight = 9999;
NSString *myString = @"lorem ipsum dolor yadda";
UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont 
                           constrainedToSize:CGSizeMake(myWidth, maxHeight)
                               lineBreakMode:textLabel.lineBreakMode];

textLabel.frame = CGRectMake(10, 10, myWidth, myStringSize.height);

我从 nevan 对上一个问题的回答中得到了这个:

参考:Apple 的 NSString UIKit 补充文档

于 2009-08-12T08:55:24.887 回答