如何在UITextView中获取一行的高度

ohh*_*hho 16 iphone uitextview

在此输入图像描述

如上图所示...... lineHeight是橙色框的高度.如何获得蓝色框的高度(即lineHeight+行间距高度)?谢谢!

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

English - adding 21 points for each line
Chinese - adding 24 points for each line
Run Code Online (Sandbox Code Playgroud)

更新(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);
Run Code Online (Sandbox Code Playgroud)

输出:

width: 41.00 height: 42.00
width: 34.00 height: 42.00
Run Code Online (Sandbox Code Playgroud)

Max*_*Max 16

你必须使用

yourTextView.font.lineHeight
Run Code Online (Sandbox Code Playgroud)