开发者

How to get height of a line in UITextView

开发者 https://www.devze.com 2023-02-11 08:59 出处:网络
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!

How to get height of a line in UITextView

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:UILineBreakM开发者_StackOverflowodeWordWrap];
NSLog(@"width: %.2f height: %.2f", rectChinese.width, rectChinese.height);

output:

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


You have to use

yourTextView.font.lineHeight


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号