简体   繁体   中英

Getting Black Italic system font from UIFont

I cannot seem to find a way to get the "SF Pro Display Heavy Italic" font.

There is UIFont.systemFont(ofSize: CGFloat, weight: UIFont.Weight) but that only gets me all non italic weights.

There is UIFont.italicSystemFont(ofSize: CGFloat) but that only gets me the "Italic Regular"

I want a specific weight of the italic system font (San Francisco Pro)

Or do get the font like a non-system font UIFont(name: String, size: CGFloat) ? That seems rather clumsy.

Take a look, I use such category for similar reasons.

extension UIFont {

     static func systemFont(ofSize: CGFloat, weight: UIFont.Weight, traits: UIFontDescriptorSymbolicTraits) -> UIFont? {
         let font = UIFont.systemFont(ofSize: ofSize, weight: weight)

         if let descriptor = font.fontDescriptor.withSymbolicTraits(traits) {
             return UIFont(descriptor: descriptor, size: 0)
         }

         return nil
     }
}

The above code does work but the one tricky part is for the thicker italic fonts, you need to use (UIFontDescriptorTraitItalic | UIFontDescriptorTraitBold) for the traits, not just UIFontDescriptorTraitItalic.

This is for Semibold, Bold, Heavy and Black fonts (all in italics).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM