在iOS上显示按比例间隔的数字(而不是单空格/表格)
通过将数字存储在NSAttributed字符串中并使用“drawAtPoint:”进行渲染,我在iOS中渲染数字(目标为7及以上)。我用的是Helvetica Neue 我注意到,像这样绘制的数字的位数不成比例——字形都具有相同的宽度。即使是瘦小的“1”也会占用与“0”相同的空间 测试证实了这一点:在iOS上显示按比例间隔的数字(而不是单空格/表格),ios,nsstring,uifont,uifontdescriptor,Ios,Nsstring,Uifont,Uifontdescriptor,通过将数字存储在NSAttributed字符串中并使用“drawAtPoint:”进行渲染,我在iOS中渲染数字(目标为7及以上)。我用的是Helvetica Neue 我注意到,像这样绘制的数字的位数不成比例——字形都具有相同的宽度。即使是瘦小的“1”也会占用与“0”相同的空间 测试证实了这一点: for(NSInteger i=0; i<10; ++i) { NSString *iString = [NSString stringWithFormat: @"%d", i]; c
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);
}
由此产生的图示符都具有相同的宽度,即6.358个点
是否有一些渲染选项可以启用,以启用比例数字图示符?是否有另一种字体(理想情况下类似于Helvetica Neue)支持比例数字字形(理想情况下,内置)?还有别的吗
谢谢。iOS 7允许您使用
UIFontDescriptor
实例指定字体。然后从描述符中获取UIFont
实例
给定UIFontDescriptor
,也可以通过使用[fontDescriptor fontDescriptor ByAddingAttributes:attibutes]
方法来获得更改某些特征的自定义,其中attributes
是字体属性的NSDictionary
苹果将这些属性记录在文档中
从参考中,一个特定的字体描述符属性UIFontDescriptorFeatureSettingsAttribute
允许您提供“表示非默认字体功能设置的字典数组。每个字典包含UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
”
UIFontFeatureTypeIdentifierKey
s和UIFontFeatureSelectorIdentifierKey
s的文档在中。比例数字的具体情况在这里有介绍,所以我把它举了起来
此代码将获取一个现有的UIFont
实例,并返回一个具有比例数字的新实例:
// 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*const existingFont=[UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFontDescriptor*常量existingDescriptor=[ExistingFontDescriptor];
NSDictionary*常量fontAttributes=@{
//下面是一组字典,每个字典都包含UIFontFeatureTypeIdentifierKey
//以及参考文献中提到的UIFontFeatureSelectorIdentifierKey。
UIFontDescriptorFeatureSettingsAttribute:@[
@{
UIFontFeatureTypeIdentifierKey:@(kNumberSpacingType),
UIFontFeatureSelectorIdentifierKey:@(KProportionalNumberSelector)
}]
};
UIFontDescriptor*常量比例描述符=[existingDescriptor FontDescriptorByAddingAttribute:fontAttributes];
UIFont*常量比例字体=[UIFont FONTWITHDESCRIPATOR:比例脚本大小:[existingFont pointSize]];
如果愿意,您可以将其作为类别添加到UIFont
,等等
编辑注:感谢Chris Schwerdt的改进。在下面的讨论中,我们讨论了比例诗句“tabular”(单空格的另一个术语)“figures”和“numbers”的用法。通过使用这些术语进行搜索,我在iOS7字体渲染中发现了这一点,我想这会让我自己回答这个问题。完成后我会更新。这张来自苹果演示文稿的pdf幻灯片提供了所需的代码。要知道的重要一点是,要使用这些功能,您必须
#import
:这就是定义符号的地方。+1别忘了为这些常量导入CoreText。谢谢Martin,我将修改代码以添加这一点,因为它最初让我挠头!UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
的值可在SFNTLayoutTypes.h
中找到,该文件将进一步引用。此外,您还可以删除kCharacterAlternativesType
键。我相信在那个时间格式示例中,它是用来将方形冒号字符替换为圆形冒号字符的,并且不会影响数字间距。只有在使用支持此功能的字体时,此选项才有效。如果您使用的是自定义字体,这可能不起作用。字体支持的选项可以通过以下方式进行检查:NSLog(@“%@”、CTFontCopyFeatures((uu桥CTFontRef)myFont))代码>
// 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]];