这似乎应该是基本上容易的,但无论出于何种原因,它都无法正常工作.我在SO上看过类似的帖子,看起来Font Traits字典可能存在问题?这是一个问题还是我完全错过了什么?这是我第一次搞乱这样的字体,所以我认为可能是后者.
我不想使用常量或提供带有"-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及更高版本.
UIFont(descriptor: imgFontDescriptor!, size: 24.0)
返回与描述符匹配的Font.如果找不到与您的描述匹配的Font,则返回默认字体.因此,您无法手动控制体重.这取决于您使用的字体.如果Font支持该权重,它将返回该权重.
还有一件事,你应该使用[UIFontDescriptorFamilyAttribute: "Helvetica"]
.因此它将根据FamilyName和您的FontWeight确定FontName.
正确的方法是使用Apple lib中的常量:
public let UIFontWeightUltraLight: CGFloat @available(iOS 8.2, *) public let UIFontWeightThin: CGFloat @available(iOS 8.2, *) public let UIFontWeightLight: CGFloat @available(iOS 8.2, *) public let UIFontWeightRegular: CGFloat @available(iOS 8.2, *) public let UIFontWeightMedium: CGFloat @available(iOS 8.2, *) public let UIFontWeightSemibold: CGFloat @available(iOS 8.2, *) public let UIFontWeightBold: CGFloat @available(iOS 8.2, *) public let UIFontWeightHeavy: CGFloat @available(iOS 8.2, *) public let UIFontWeightBlack: CGFloat*/
您应该使用http://iosfonts.com/来确定该系列名称支持的字体粗细.
如果是Helvetica:
let traits = [UIFontWeightTrait: UIFontWeightLight] // UIFontWeightBold / UIFontWeightRegular let imgFontDescriptor = UIFontDescriptor(fontAttributes: [UIFontDescriptorFamilyAttribute: "Helvetica"]) imgFontDescriptor = imgFontDescriptor.fontDescriptorByAddingAttributes([UIFontDescriptorTraitsAttribute: traits])
如果使用现有的fontDescriptor(例如,来自现有字体的字体),则由于fontDescriptor.fontAttributes中的显式“ .name”属性,它可能无法工作。
解决方案,对我有用(快速4):
extension UIFont { var bold: UIFont { return withWeight(.bold) } var semibold: UIFont { return withWeight(.semibold) } private func withWeight(_ weight: UIFont.Weight) -> UIFont { var attributes = fontDescriptor.fontAttributes var traits = (attributes[.traits] as? [UIFontDescriptor.TraitKey: Any]) ?? [:] traits[.weight] = weight attributes[.name] = nil attributes[.traits] = traits attributes[.family] = familyName let descriptor = UIFontDescriptor(fontAttributes: attributes) return UIFont(descriptor: descriptor, size: pointSize) } }
用法:
let baseFont = UIFont(name: "American Typewriter", size: 20)! let boldFont = baseFont.bold let semibold = baseFont.semibold
使用Swift 4.1,只需执行以下操作:
var descriptor = UIFontDescriptor(name: "Helvetica Neue", size: 24.0) descriptor = descriptor.addingAttributes([UIFontDescriptor.AttributeName.traits : [UIFontDescriptor.TraitKey.weight : UIFont.Weight.light]]) let font = UIFont(descriptor: descriptor, size: 24.0)