Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new San Francisco font in iOS 9?

Tags:

ios

ios9

fonts

People also ask

What font is iOS 9?

iOS 9 uses SanFranciscoUIDisplay and SanFranciscoText as their default font.

Can I use Apple San Francisco font?

So, you can't use it, unless you only use it on your website to showcase your iOS/OSX app, in which case it is free to use.

Where can I use San Francisco font?

Normally Apple's font can not be used on any webpage through CSS. These kind of fonts are in-build in Apple's product but the new San Francisco font can be used in any web content.


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)

Swift 4

label.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.bold)

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)!