我使用下面的代码来计算宽度有限的字符串行数.
NSString *text = @"abcdefgh";
UIFont *font = [UIFont systemFontOfSize:15.0];
CGFloat h = [text suggestHeightWithFont:font width:320.0];
NSInteger lines = (h > font.lineHeight) ? h/font.lineHeight+1 : h/font.lineHeight;
NSLog(@"height %f, %f, number lines:%ld", h, font.lineHeight, (unsigned long)lines);
Run Code Online (Sandbox Code Playgroud)
但我发现font.lineHeight(日志显示它是17.900391)大于设置为的字体大小15.0.
日志消息显示:
身高17.900391,17.900391,数字线:1
例如,对于字体大小10
A可能有ascender = 10,descender = 0
g可能有ascender = 5,descender = 4
lineHeight = 14 +领先... 14,5也许
请注意,此处的数字是随机的,仅用于更好地说明问题
| 归档时间: |
|
| 查看次数: |
2927 次 |
| 最近记录: |