在iOS上按比例显示数字(而不是等宽/表格) [英] Displaying proportionally spaced numbers (instead of monospace / tabular) on iOS
问题描述
我通过将数字存储在NSAttributedString中并使用"drawAtPoint:"进行渲染,从而在iOS中渲染数字(目标为7及更高版本).我正在使用Helvetica Neue.
I am rendering numbers in iOS (targeting 7 and up) by storing them in an NSAttributedString and rendering with "drawAtPoint:". I am using Helvetica Neue.
我注意到,这样绘制的数字位数不是成比例的 –所有字形都具有相同的宽度.即使是瘦的"1"也要占用与"0"相同的空间.
I have noticed that digits of numbers drawn like this are not proportional – the glyphs all have the same width. Even a skinny "1" takes up the same space as a "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点.
This resulting glyphs all have the same width of 6.358 points.
我是否可以启用某些渲染选项以启用比例数字字形?是否有另一种字体(理想情况下类似于Helvetica Neue)支持比例数字字形(理想情况下是内置的)?还有吗?
Is there some rendering option I can turn on that to enable proportional digit glyphs? Is there another font (ideally similar to Helvetica Neue) that supports proportional digit glyphs (ideally, built in)? Anything else?
谢谢.
推荐答案
iOS 7允许您使用UIFontDescriptor
实例指定字体.然后从描述符中获得一个UIFont
实例.
iOS 7 lets you specify fonts using UIFontDescriptor
instances. A UIFont
instance is then obtained from a descriptor.
考虑到UIFontDescriptor
,还可以通过使用[fontDescriptor fontDescriptorByAddingAttributes: attibutes]
方法(其中attributes
是字体属性的NSDictionary
)来更改其某些特征,对其进行定制.
Given a UIFontDescriptor
it is also possible to obtain a customisation of it that changes some characteristics by using the method [fontDescriptor fontDescriptorByAddingAttributes: attibutes]
where attributes
is an NSDictionary
of font attributes.
Apple在 UIFontDescriptor
参考中记录属性. >.
Apple documents the attributes in the UIFontDescriptor
reference.
从参考文献中,可以使用一个特定的字体描述符属性UIFontDescriptorFeatureSettingsAttribute
提供一组表示非默认字体功能设置的词典.每个词典都包含UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
."
From the reference, one particular font descriptor attribute UIFontDescriptorFeatureSettingsAttribute
lets you provide "An array of dictionaries representing non-default font feature settings. Each dictionary contains UIFontFeatureTypeIdentifierKey
and UIFontFeatureSelectorIdentifierKey
."
UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
的文档位于 Apple的字体注册表文档中.苹果演示文稿幻灯片的 pdf中涵盖了比例数字的具体情况,所以我刚刚举起了.
Documentation of the UIFontFeatureTypeIdentifierKey
s and UIFontFeatureSelectorIdentifierKey
s is in Apple's Font Registry documentation. The specific case of proportional digits is covered in this pdf of slides of an Apple presentation, so I just lifted that.
此代码将采用现有的UIFont
实例,并带给您具有比例数字的新实例:
This code that will take an existing UIFont
instance and give you back a new instance with proportional digits:
// You'll need this somewhere at the top of your file to pull
// in the required constants.
#import <CoreText/CoreText.h>
…
UIFont *const existingFont = [UIFont preferredFontForTextStyle: UIFontTextStyleBody];
UIFontDescriptor *const existingDescriptor = [existingFont fontDescriptor];
NSDictionary *const fontAttributes = @{
// Here comes that array of dictionaries each containing UIFontFeatureTypeIdentifierKey
// and UIFontFeatureSelectorIdentifierKey that the reference mentions.
UIFontDescriptorFeatureSettingsAttribute: @[
@{
UIFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
UIFontFeatureSelectorIdentifierKey: @(kProportionalNumbersSelector)
}]
};
UIFontDescriptor *const proportionalDescriptor = [existingDescriptor fontDescriptorByAddingAttributes: fontAttributes];
UIFont *const proportionalFont = [UIFont fontWithDescriptor: proportionalDescriptor size: [existingFont pointSize]];
如果愿意,可以将其添加为UIFont
上的类别,等等.
You could add this as a category on UIFont
if you wished, etc.
编辑说明:感谢Chris Schwerdt所做的改进.
Edit note: thanks to Chris Schwerdt for the improvements.
这篇关于在iOS上按比例显示数字(而不是等宽/表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!