iOS Swift为字体描述符添加自定义权重

时间:2016-01-04 03:16:03

标签: ios swift fonts uifontdescriptor

这看起来应该很简单,但无论出于何种原因,它都无法正常工作。我在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)

研究

Font Descriptor returns nil in iOS 8

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及更高版本。

5 个答案:

答案 0 :(得分:11)

UIFont(descriptor: imgFontDescriptor!, size: 24.0)返回与描述符匹配的Font。如果找不到与您的描述匹配的Font,则返回默认字体。因此,您无法手动控制体重。这取决于您使用的字体。如果Font支持该权重,它将返回该值。

还有一件事,你应该使用[UIFontDescriptorFamilyAttribute: "Helvetica"]。所以它将根据FamilyName&amp ;;确定FontName。你的FontWeight。

正确的方法是使用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])

答案 1 :(得分:6)

如果您使用现有的fontDescriptor(例如来自现有字体),它可能无效,因为明确的' .name' fontDescriptor.fontAttributes中的属性。

解决方案,对我有用(Swift 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

答案 2 :(得分:6)

使用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)

答案 3 :(得分:5)

简洁的扩展名:

extension UIFont {
  /// Returns a new font with the weight specified
  /// - Parameter weight: The new font weight
  func withWeight(_ weight: UIFont.Weight) -> UIFont {
    let newDescriptor = fontDescriptor.addingAttributes([.traits: [
      UIFontDescriptor.TraitKey.weight: weight]
    ])
    return UIFont(descriptor: newDescriptor, size: pointSize)
  }
}

答案 4 :(得分:2)

这是Apple在其CareKit框架中对UIFont+Extensions.swift的扩展:

https://github.com/carekit-apple/CareKit/

extension UIFont {
    static func preferredCustomFont(forTextStyle textStyle: TextStyle, weight: Weight) -> UIFont {
        let defaultDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)
        let size = defaultDescriptor.pointSize
        let fontDescriptor = UIFontDescriptor(fontAttributes: [
            UIFontDescriptor.AttributeName.size: size,
            UIFontDescriptor.AttributeName.family: UIFont.systemFont(ofSize: size).familyName
        ])

        // Add the font weight to the descriptor
        let weightedFontDescriptor = fontDescriptor.addingAttributes([
            UIFontDescriptor.AttributeName.traits: [
                UIFontDescriptor.TraitKey.weight: weight
            ]
        ])
        return UIFont(descriptor: weightedFontDescriptor, size: 0)
    }
}