这似乎应该很容易,但无论出于何种原因,它都不起作用。我在 SO 上读过类似的帖子,似乎字体特征字典可能有问题?这是一个问题还是我完全错过了什么?这是我第一次弄乱这样的字体,所以我认为可能是后者。
我不想使用常量或提供带有“-bold”变体的字体。我想对字体粗细进行细粒度控制。
let traits = [UIFontWeightTrait : 1.0]
imgFontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorNameAttribute: "Helvetica"])
imgFontDescriptor = imgFontDescriptor.fontDescriptorByAddingAttributes([UIFontDescriptorTraitsAttribute:traits])
imgTextFont = UIFont(descriptor: imgFontDescriptor!, size: 24.0)
研究:
- 字体描述符在 iOS 8 中返回 nil
- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIFontDescriptor_Class/#//apple_ref/doc/constant_group/Font_Traits_Dictionary_Keys
根据 iOS 文档,使用数字值应该可以工作(我正在运行 iOS 9.2):
UIFontWeightTrait
作为 NSNumber 对象的标准化权重值。有效值范围是 -1.0 到 1.0。0.0 的值对应于常规或中等字体粗细。您还可以使用字体粗细常量来指定特定的粗细;有关可以使用的常量列表,请参阅字体粗细。在 iOS 7.0 及更高版本中可用。