【发布时间】:2013-11-26 02:26:11
【问题描述】:
我通过将数字存储在 NSAttributedString 中并使用“drawAtPoint:”渲染来在 iOS 中渲染数字(针对 7 及以上)。我正在使用 Helvetica Neue。
我注意到像这样绘制的数字的位数不成比例 - 字形都具有相同的宽度。即使是细小的“1”也与“0”占用相同的空间。
测试证实了这一点:
for(NSInteger i=0; i<10; ++i)
{
NSString *iString = [NSString stringWithFormat: @"%d", i];
const CGSize iSize = [iString sizeWithAttributes: [self attributes]];
NSLog(@"Size of %d is %f", i, iSize.width);
}
在其他地方:
-(NSDictionary *) attributes
{
static NSDictionary * attributes;
if(!attributes)
{
attributes = @{
NSFontAttributeName: [UIFont systemFontOfSize:11],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
}
return attributes;
}
生成的字形都具有相同的宽度,均为 6.358 磅。
我可以打开一些渲染选项来启用比例数字字形吗?是否有另一种字体(理想情况下类似于 Helvetica Neue)支持比例数字字形(理想情况下是内置的)?还有什么?
谢谢。
【问题讨论】:
-
这个来自 Apple 演示文稿的 pdf link 幻灯片提供了所需的代码。重要的是要知道要使用这些功能,您必须
#import <CoreText/CoreText.h>:这是定义符号的地方。
标签: ios nsstring uifont uifontdescriptor