2014-02-06 15 views
3

我想知道如何知道如何定義CGRect尺寸的UILabel基於字符串和字體大小的大小。iOS如何確定動態UILabel的大小

在這裏我有什麼,直到知道:

UILabel *myLabel = [[UILabel alloc] init]; 
myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0]; 
myLabel.text = @"This is a string example" 

問題是我沒有更多鈔票的字符串列表,但是,需要到中心的標籤和字符串不應該印章(使用刺上面的例子:「這是一個str ...」)。你們誰都知道如何確定UILabel基於字符串和字體大小的大小?

我真的很感謝你的幫助。

+0

檢查這個問題:http://stackoverflow.com/questions/19128797/calculating-uilabel-text-size – leparlon

回答

5

只使用

UILabel *myLabel = [[UILabel alloc] init]; 
myLabel.font = [UIFont fontWithName:@"BentonSansComp-Medium" size:50.0]; 
myLabel.text = @"This is a string example" 
[myLabel sizeToFit]; //it will resize the label to just the text is set 
CGRectMake size = myLabel.frame; // u will get the frame of the label once is resized 
float height = size.size.height; //to get the height of the label 
float width = size.size.width; //to get the width of the label 

希望它有助於

確保evrytime更改文本ü致電[myLabel sizeToFit]方法

+0

我試過你的代碼,但我得到的size.height和size.width都等於cero – user2924482

+1

你是否已經將文本設置爲標籤?確保標籤具有文本XD –

0

我認爲你需要的是多條線,這將增加下面所需的面積。按照這一點,將不會有...

myLabel.lineBreakMode = NSLineBreakByWordWrapping; 
myLabel.numberOfLines = 0; 
+0

但仍需要知道的UILabel – user2924482

0

您可以通過這樣計算標籤的width

[@"STRING" sizeWithFont:[UIFont fontWithName:@"FONT" size:12] forWidth:200 lineBreakMode:NSLineBreakByWordWrapping]; 

否則,你可以只設置在標籤的字符串,然後調用sizeToFit,這將設置標籤的基礎上,在frame文本。

+0

的尺寸 - (CGSize)sizeWithFont:(UIFont * )字體forWidth:(CGFloat)寬度lineBreakMode:(NSLineBreakMode)lineBreakMode NS_DEPRECATED_IOS(2_0,7_0,「Use -boundingRectWithSize:options:attributes:context:」); – user2924482

0

在iOS7中使用NSString方法boundingRectWithSize:options:context:。看文檔here

CGRect rectYouNeed = 
    [myLabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) 
          options:NSStringDrawingUsesLineFragmentOrigin 
          attributes:@{ NSFontAttributeName : myLabel.font } 
          context:nil]; 
1

剛剛有同樣的問題。計算很多尺寸的UILabel s或UITextView s很昂貴,所以我想知道是否有比配置標籤或文本視圖更便宜的方法,然後詢問其大小。計算長表視圖的行高時,這一點尤爲重要。

原來,有:UILabel似乎構建了UIKit的佈局和繪圖的字符串添加,而UITextView使用Text Kit(在iOS 7中)。這兩個工具都可以配置爲使用相同的設置並使用UIKit類更便宜地計算字符串尺寸。

對於這裏UILabel的我發現了什麼:

CGFloat labelHeight = [testString boundingRectWithSize:(CGSize){ labelWidth, CGFLOAT_MAX } 
               options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:@{ NSFontAttributeName : labelFont }; 
               context:nil].size.height; 
labelHeight = ceil(labelHeight); 

這是假設numberOfLines = 0。請注意需要總結結果。

我測試了上面的代碼,大約4000個字符串呈現爲100個不同的寬度,所有結果都等於我用於比較的UILabel。

我做了同樣的計算尺寸UITextView這是更復雜,並需要建立一個NSLayoutManager。在這裏,加速更令人印象深刻(比使用UITextView快50倍)。

如果有人感興趣的話,這裏是code and test project

0

檢查我的代碼,我已經寫在我的應用程序之一。

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX); 
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize)); 
//adjust the label the the new height. 
CGRect newFrame = myLabel.frame;//get initial frame of your label 
newFrame.size.height = expectedLabelSize.height; 
myLabel.frame = newFrame; 
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame)); 
[myLabel setText:attString]; 
+0

sizeWithFont已被棄用,boundingRectWithSize:options:attributes:context方法是更新的替換。歡呼 –

+0

謝謝,這段代碼有點老了。我在1年前寫了它來更新標籤尺寸。 – Pawan

相關問題