如何获取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
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须使用
You have to use
尝试使用
NSString
UIKit< /code> 添加
。
-sizeWithFont:
的一些变体应该是您所需要的。Try using the
NSString
UIKit
additions. Some variation of-sizeWithFont:
should be what you need.