看起来这应该是一件基本容易的事情,但出于某种原因它并没有起作用。我在 Stack Overflow 上读了一些类似的帖子,似乎字体特征词典可能存在问题?这是个问题还是我完全忽略了什么?这是我第一次尝试这样的字体处理,所以我想可能是后者。
我不想使用常量或提供一个带有“-bold”变体的字体。我希望对字体重量有精细的控制。
let traits = [UIFontWeightTrait : 1.0]
imgFontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorNameAttribute: "Helvetica"])
imgFontDescriptor = imgFontDescriptor.fontDescriptorByAddingAttributes([UIFontDescriptorTraitsAttribute:traits])
imgTextFont = UIFont(descriptor: imgFontDescriptor!, size: 24.0)
研究:
- Font Descriptor returns nil in iOS 8(在iOS 8中字体描述符返回nil)
- https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIFontDescriptor_Class/#//apple_ref/doc/constant_group/Font_Traits_Dictionary_Keys(UIFontDescriptor类的文档,包括字体特征词典键常量)
iOS文档中指出,使用数字值应该可以工作(我正在运行iOS 9.2):
UIFontWeightTrait(UIFont重量特征)
规格化的权重值作为NSNumber对象。有效值范围为-1.0到1.0。值0.0对应于常规或中等字重。您还可以使用字重常量来指定特定的字重;有关可用常量的列表,请参见字重。自iOS 7.0以后可用。