55

Before iOS 9 to reference fonts we used fontWithName of UIFont:

[UIFont fontWithName:@"HelveticaNeue" size:18]

Now we're moving to iOS 9. How to reference a new San Francisco font in the same way?

We can use it with systemFontOfSize of UIFont, but how to reference styles other than regular? For example, how to use San Francisco Medium or San Francisco Light fonts?

alexey
  • 8,360
  • 14
  • 70
  • 102
  • This kind of sounds like a thing that is going to be in the iOS9 developer docs. Have you looked there yet? (If so, and it's not in there, that's worth putting in your answer. If not, step one is to look there) – Mike 'Pomax' Kamermans Jul 12 '15 at 16:46

3 Answers3

121

In iOS 9 it is the system font, so you could do:

let font = UIFont.systemFontOfSize(18)

You can use the font name directly, but I don't think this is safe:

let font = UIFont(name: ".SFUIText-Medium", size: 18)!

You can also create the font with specific weight, like so:

let font = UIFont.systemFontOfSize(18, weight: UIFontWeightMedium)

or

let font = UIFont.systemFontOfSize(18, weight: UIFontWeightLight)
MirekE
  • 11,515
  • 5
  • 35
  • 28
  • 1
    Any official ways to use Medium or Light? – alexey Jul 12 '15 at 16:55
  • 2
    Just to emphasize: MirekE is correct, it is not safe to use `.SFUIText-Medium` or any other exact San Fransisco name. Apple has stated that those names are private and subject to change. They want you to use the system font methods built into `UIFont` and `NSFont`, specifically because they reserve the right to modify what exactly the system font is, depending on the user's situation. – justinpawela Jul 12 '15 at 17:10
  • 7
    Of course, if you _really_ need a font name, `let fontName = UIFont.systemFontOfSize(18, weight: UIFontWeightLight).fontName` , so that if the private name changes, you automatically retrieve the right one. – magma Sep 16 '15 at 00:03
  • i'm using SF weightlight font on iOS 9 to make a custom keyboard, but the trouble is it looks lighter than default font. i've tried to switch regular and medium font, but it quite not like the text on default iOS keyboard?! – TomSawyer Oct 22 '15 at 10:18
  • is there any way to use this font on a UIWebView? – nhenrique Nov 19 '15 at 09:40
8

Swift 4

label.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.bold)
Marcos Dávalos
  • 501
  • 1
  • 5
  • 14
2

Details

  • Xcode Version 10.2.1 (10E1001), Swift 5

Solution

import UIKit

extension UIFont {

    enum Font: String {
        case SFUIText = "SFUIText"
        case SFUIDisplay = "SFUIDisplay"
    }

    private static func name(of weight: UIFont.Weight) -> String? {
        switch weight {
            case .ultraLight: return "UltraLight"
            case .thin: return "Thin"
            case .light: return "Light"
            case .regular: return nil
            case .medium: return "Medium"
            case .semibold: return "Semibold"
            case .bold: return "Bold"
            case .heavy: return "Heavy"
            case .black: return "Black"
            default: return nil
        }
    }

    convenience init?(font: Font, weight: UIFont.Weight, size: CGFloat) {
        var fontName = ".\(font.rawValue)"
        if let weightName = UIFont.name(of: weight) { fontName += "-\(weightName)" }
        self.init(name: fontName, size: size)
    }
}

Usage

guard let font = UIFont(font: .SFUIText, weight: .light, size: 14) else { return }

// ...

let font = UIFont(font: .SFUIDisplay, weight: .bold, size: 17)!
Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127