2014-10-28 82 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