2014-10-28 80 views
0

我以編程方式創建一些文本。
文本獲取屬性,然後返回到視圖控制器顯示的位置。
本來我創建具有最大高度的矩形的UILabel 40
因此,如果文本太大的字體大小減小通過
申請文本adjustsFontSizeToFitWidth以適應矩形。
如果文字很小,矩形中有很多空白空間(在上面和下面)。
是否有可能在這一點上得到最小的矩形eclosing我的文字。
謝謝計算UIlabel中圍繞文本的最小矩形

NSAttributedString * Text=[circleModel.Selected_set objectForKey:@"sentence_text"]; 
CGRect recty; 
recty= CGRectMake(mainScreen.size.width*0.55, 100, mainScreen.size.width*0.43,40); 

UILabel *Latex_text = [[UILabel alloc] initWithFrame:recty]; 
Latex_text.AttributedText = Text; 
Latex_text.numberOfLines = 0; 

Latex_text.adjustsFontSizeToFitWidth = YES; 
Latex_text.textAlignment = NSTextAlignmentCenter; 
[self.view addSubview:Latex_text]; 
//Latex_text.backgroundColor = [UIColor whiteColor]; 

回答

0

怎麼樣使用textRectForBounds?如果adjustsFontSizeToFitWidth不適合它,它應該給你至少一個估計。

UILabel *Latex_text = [[UILabel alloc] init]; 
Latex_text.AttributedText = Text; 
Latex_text.numberOfLines = 0; 

Latex_text.adjustsFontSizeToFitWidth = YES; 
Latex_text.textAlignment = NSTextAlignmentCenter; 
recty.size = [Latex_text textRectForBounds:recty limitedToNumberOfLines:0].size; 
[Latex_text setFrame:recty]; 
+0

幹得好伊恩。謝謝 – Vaki 2014-10-28 18:44:45