2013-02-13 87 views
2

我遇到了一些UITextViews的問題。您可以選擇更改我的應用中的字體大小,當您更改它時,它會重新加載內容並調整文本查看的大小,因爲視圖中有多個字體大小。我使用下面的代碼來進行大小調整,但它有時會返回正確的高度,但是當我在應用程序中更改文本大小時,它似乎分解並返回不正確的高度,並且textviews中的文本正在切斷底端。有人能幫我嗎?sizeWithFont:constrainedToSize:lineBreakMode:更改字體大小時無法正常工作

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

回答

1

我知道這是一個老問題&末的答案,但它仍然是非常相關的,

這sizeWithFont方法現在已經過時,這種新的方法效果最佳

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; 

所以,你可以調整您的標籤(或表格單元等)到

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

我希望th是幫助,歡呼,吉姆。