Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get height of a line in UITextView

enter image description here

As shown in above diagram ... the lineHeight is the height of the orange box. How can I get the height of the blue box (i.e. lineHeight + line spacing height)? Thanks!

UPDATE: I found the the lineHeight behaves differently depends on the characters in content. If the content is all in English, the lineHeight is corrent (21, for example, in the default UITextView). However, when the content is mixed with Chinese characters, the UIFont lineHeight is still reported as 21 while the self.textView.contentSize.height is increased differently:

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);

output:

width: 41.00 height: 42.00
width: 34.00 height: 42.00
like image 997
ohho Avatar asked Feb 18 '11 01:02

ohho


2 Answers

You have to use

yourTextView.font.lineHeight
like image 120
Max Avatar answered Nov 01 '22 05:11

Max


Try using the NSString UIKit additions. Some variation of -sizeWithFont: should be what you need.

like image 31
Jeff Kelley Avatar answered Nov 01 '22 04:11

Jeff Kelley