skip to Main Content

I am writing an iOS app in Swift, and I want to use the system font, SF Pro. However, I can’t seem to figure out how to set the font weight at the same time as making the text italic. I know this was possible with Helvetica Neue with

UIFont(name: "HelveticaNeue-UltraLightItalic", size: 24.0)

But when using the system font, I can only seem to do one or the other like this

UIFont.italicSystemFont(ofSize: 24)

or

UIFont.systemFont(ofSize: 75.0, weight: UIFontWeightThin)

I have seen SF Pro Display Thin Italic in Font Book, and I know you can use it in editing apps like Sketch and Photoshop, but I have yet to find a way to get iOS to display it.

4

Answers


  1. Here is one method:

        var fnt = UIFont.systemFont(ofSize: 75.0, weight: UIFontWeightThin)
        if let dsc = fnt.fontDescriptor.withSymbolicTraits(.traitItalic) {
            fnt = UIFont(descriptor: dsc, size: 0)
        }
        labelOne.font = fnt
    

    That should give you something to build on – such as turning it into an extension to make it flexible.

    Login or Signup to reply.
  2. UIFont(name: "SanFranciscoText-LightItalic", size: 24.0)
    

    In general, use this site for fonts: http://iosfonts.com/

    Login or Signup to reply.
  3. Found this extension here

    extension UIFont {
    
    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont? {
        guard let descriptorL = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) 
        else{
            return nil
        }
        return UIFont(descriptor: descriptorL, size: 0)
    }
    
    func boldItalic() -> UIFont? {
        return withTraits(traits: .traitBold, .traitItalic)
    }
    

    Use the extension as:

    label.font = label.font.boldItalic()
    
    Login or Signup to reply.
  4. As (an ugly) on-liner:

    UIFont(descriptor: UIFont.systemFont(ofSize: 14, weight: .thin).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 14)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search