4

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.

Evan Green
  • 53
  • 1
  • 4

4 Answers4

5

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.

DonMag
  • 44,662
  • 5
  • 32
  • 56
2

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()
SrB
  • 343
  • 4
  • 11
1

As (an ugly) on-liner:

UIFont(descriptor: UIFont.systemFont(ofSize: 14, weight: .thin).fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 14)
peralmq
  • 1,581
  • 15
  • 18
-1
UIFont(name: "SanFranciscoText-LightItalic", size: 24.0)

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

Max Pevsner
  • 3,842
  • 2
  • 15
  • 32