iOS 7 允许您使用指定字体UIFontDescriptor
实例。 AUIFont
然后从描述符中获取实例。
Given a UIFontDescriptor
还可以使用以下方法对其进行定制,以更改某些特征[fontDescriptor fontDescriptorByAddingAttributes: attibutes]
where attributes
is an NSDictionary
字体属性。
Apple 文档中的属性UIFontDescriptor参考 https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIFontDescriptor_Class/Reference/Reference.html.
从参考文献中,一个特定的字体描述符属性UIFontDescriptorFeatureSettingsAttribute
允许您提供“代表非默认字体功能设置的字典数组。每个字典包含UIFontFeatureTypeIdentifierKey
and UIFontFeatureSelectorIdentifierKey
."
的文档UIFontFeatureTypeIdentifierKey
s and UIFontFeatureSelectorIdentifierKey
s is in Apple 的字体注册表文档 https://developer.apple.com/fonts/registry/。本节涵盖了比例数字的具体情况Apple 演示文稿的 pdf 幻灯片 http://docs.huihoo.com/apple/wwdc/2013/session_223__using_fonts_with_text_kit.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]];
您可以将其添加为类别UIFont
如果你愿意的话等等
编辑说明:感谢 Chris Schwerdt 的改进。