2

我对一些 UITextViews 有一些问题。您可以选择更改我的应用程序中的字体大小,当您更改它时,它会重新加载内容并调整文本视图的大小,因为视图中有多个。我正在使用下面的代码来调整大小,但它有时会返回正确的高度,但是当我在应用程序中更改文本大小时,它似乎会崩溃并返回不正确的高度,并且 textviews 中的文本在底部。有人能帮我吗?

int height = [tempTextView.text sizeWithFont:
[UIFont fontWithName:fontFamily size:fontSizeF] 
constrainedToSize:CGSizeMake(300, 10000) lineBreakMode:
NSLineBreakByWordWrapping].height;
4

1 回答 1

1

I know this is an old question & late answer, but it's still very relevant,

This sizeWithFont method is now deprecated, this new method works best

NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(390, 1000);

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName];

CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

So you can adjust your label (or table cell etc) to

label.frame.size.height = newExpectedLabelSize.height;

I hope this helps, cheers, Jim.

于 2014-01-26T10:51:08.777 回答