2011-02-18 31 views
14

enter image description here如何取得一行的高度的UITextView

如上图表所示...的lineHeight是橙色盒子的高度。我怎样才能得到蓝框的高度(即lineHeight +行间距高度)?谢谢!

更新:我发现lineHeight行为有所不同取决于内容中的字符。如果内容全部是英文,那么lineHeight是正确的(21,例如,在默认的UITextView中)。然而,当内容与中国文字混合时,UIFont lineHeight仍报告为21,而self.textView.contentSize.height是不同的增加:

English - adding 21 points for each line 
Chinese - adding 24 points for each line 

UPDATE(sizeWithFont:

CGSize constrainedRect = CGSizeMake(1000, 1000); 
CGSize rectEnglish = [@"hello\nworld" sizeWithFont:self.textView.font constrainedToSize:constrainedRect lineBreakMode:UILineBreakModeWordWrap]; 
NSLog(@"width: %.2f height: %.2f", rectEnglish.width, rectEnglish.height); 
CGSize rectChinese = [@"你\n好嗎" sizeWithFont:self.textView.font constrainedToSize:constrainedRect lineBreakMode:UILineBreakModeWordWrap]; 
NSLog(@"width: %.2f height: %.2f", rectChinese.width, rectChinese.height); 

输出:

width: 41.00 height: 42.00 
width: 34.00 height: 42.00 

回答

12

您必须使用

yourTextView.font.lineHeight 
+1

请注意`UIFont.lineHeight`仅限iOS4.0 +。 – 2011-02-18 04:27:15

+1

这是一个只读属性 – Puran 2012-03-14 00:39:27