问题描述
您可以向 UIFont
实例询问其 lineHeight
指标:
You can ask a UIFont
instance for its lineHeight
metric:
UIFont *const font = [UIFont systemFontOfSize: 10];
CGFloat lineHeight = [font lineHeight];
如果我想要特定的lineHeight(例如20),如何创建具有该大小的UIFont?
If I want a particular lineHeight, (20, say), how can I create a UIFont with that size?
推荐答案
答案
令我惊讶的是,我的有限分析表明可以使用线性插值.这是 UIFont
上的类别,它将执行所需的操作:
Answer
My limited analysis suggests, surprisingly to me, that linear interpolation can be used. Here's a category on UIFont
that will do what's needed:
@interface UIFont (FontWithLineHeight)
+(UIFont*) systemFontWithLineHeight: (CGFloat) lineHeight;
@end
具有实现方式:
@implementation UIFont (FontWithLineHeight)
+(UIFont*) systemFontWithLineHeight:(CGFloat)lineHeight
{
const CGFloat lineHeightForSize1 = [[UIFont systemFontOfSize: 1] lineHeight];
const CGFloat pointSize = lineHeight / lineHeightForSize1;
return [UIFont systemFontOfSize: pointSize];
}
@end
像这样使用:
UIFont *const font = [UIFont systemFontWithLineHeight: 20];
NSLog(@"%f", [font lineHeight]);
哪个输出:
2014-01-08 16:04:19.614 SpikeCollectionView[6309:60b] 20.000000
要求的是什么.
似乎 UIFont
的 lineHeight
与 pointSize
呈线性比例关系.换句话说,如果将 pointSize
设置为两倍,则 lineHeight
的设置也将为两倍.如果将 pointSize
减半,则还将 lineHeight
减半.这意味着可以使用插值来找到将提供给定 lineHeight
或其他指标的 pointSize
.
It seems that the lineHeight
of a UIFont
scales linearly with the pointSize
. In other words, if you make the pointSize
twice as much, then the lineHeight
will be twice as much too. If you halve the pointSize
you also halve the lineHeight
. This means that interpolation can be used to find the pointSize
that will provide a given lineHeight
or other metric.
以下代码显示线性度:
const CGFloat lineHeight1 = [[UIFont systemFontOfSize: 1] lineHeight];
const CGFloat lineHeight10 = [[UIFont systemFontOfSize: 10] lineHeight];
const CGFloat lineHeight100 = [[UIFont systemFontOfSize: 100] lineHeight];
const CGFloat ratio1_10 = lineHeight10 / lineHeight1;
const CGFloat ratio10_100 = lineHeight100 / lineHeight10;
NSLog(@"%f", ratio1_10);
NSLog(@"%f", ratio10_100);
输出为:
2014-01-08 15:56:39.326 SpikeCollectionView[6273:60b] 9.999999
2014-01-08 15:56:39.329 SpikeCollectionView[6273:60b] 10.000001
警告
我仅针对iOS 7上的系统字体对此进行了测试.其他字体在缩放至它们的pointSize时可能无法显示其指标的线性缩放.如果有人可以确认这是否可以保证或什么时候不起作用,那将是奇妙的.如果您尝试将其用于其他 UIFont
,但此方法不起作用,请注释或扩展此答案,或添加另一个答案.
Caution
I've only tested this for the system font on iOS 7. Other fonts may not exhibit linear scaling of their metrics under scalings to their pointSize. If someone could confirm whether this is guaranteed or when it won't work, that would be marvellous. If you try this for other UIFont
and it doesn't work, please comment or extend this answer, or add another.
如果不能保证指标的线性缩放,则必须搜索所需的 pointSize
.您将需要一个求根"数字算法.伊利诺伊算法适用于此.
If linear scaling of the metrics is not guaranteed, it would be necessary to search for the required pointSize
. You will need a "root finding" numeric algorithm. The Illinois Algorithm would work for this.
这篇关于如何找到获得所需的lineHeight或其他指标所需的UIFont pointSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!