在iOS上显示按比例分隔的数字(而不是等宽/表格)

Ben*_*ohn 7 nsstring uifont ios uifontdescriptor

我通过将它们存储在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);
}
Run Code Online (Sandbox Code Playgroud)

在其他地方:

-(NSDictionary *) attributes
{
  static NSDictionary * attributes;
  if(!attributes)
  {
    attributes = @{
                   NSFontAttributeName: [UIFont systemFontOfSize:11],
                   NSForegroundColorAttributeName: [UIColor whiteColor]
                   };
  }
  return attributes;
}
Run Code Online (Sandbox Code Playgroud)

这个结果字形都具有 6.358点的相同宽度.

是否有一些渲染选项我可以启用它来启用比例数字字形?是否有另一种字体(理想情况下类似于Helvetica Neue)支持比例数字字形(理想情况下,内置)?还要别的吗?

谢谢.

Ben*_*ohn 18

iOS 7允许您使用UIFontDescriptor实例指定字体.甲UIFont实例然后从描述符获得的.

给定a UIFontDescriptor也可以通过使用[fontDescriptor fontDescriptorByAddingAttributes: attibutes]其中attributesNSDictionary字体属性的方法来获得它的自定义,从而改变某些特征.

Apple记录了UIFontDescriptor参考中的属性.

从引用中,一个特定的字体描述符属性UIFontDescriptorFeatureSettingsAttribute允许您提供"表示非默认字体功能设置的字典数组.每个字典包含UIFontFeatureTypeIdentifierKeyUIFontFeatureSelectorIdentifierKey."

该文件的UIFontFeatureTypeIdentifierKeyS和UIFontFeatureSelectorIdentifierKeyS是在苹果的字体注册表文件.这个比例数字的具体情况在Apple演示文稿的幻灯片的pdf中有所涉及,所以我刚刚解除了这一点.

此代码将采用现有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]];
Run Code Online (Sandbox Code Playgroud)

UIFont如果您愿意,可以将其添加为类别等.

编辑说明:感谢Chris Schwerdt的改进.

  • `UIFontFeatureTypeIdentifierKey`和`UIFontFeatureSelectorIdentifierKey`的值可以在`SFNTLayoutTypes.h`中找到,它进一步引用了[Apple的Font Registry文档](https://developer.apple.com/fonts/registry/). (2认同)
  • 此外,您可以删除`kCharacterAlternativesType`键.我相信它在那个时间格式化示例中用于用圆角冒号替换方形冒号字符,并且不影响数字间距. (2认同)