Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System font for both iOS 8 and iOS 9

I want to support both iOS 8 and iOS 9 systems for my app. And maybe iOS 7. As we know, system font for iOS 7 and 8 is Helvetica Neue. But in iOS 9 system font is San-Francisco. And if you don't set Helvetica font explicitly via [UIFont fontWithName:@"HelveticaNeue" size:15];, but use [UIFont systemFontOfSize:15];, you'll get Helvetica for iOS 7 and 8 and San-Francisco for iOS 9 automatically. And it's great! For interface builder's labels and buttons you can set thin, ultra thin, medium etc. system fonts. It is great too. But how can I set these thin, ultra, medium system fonts in code, programmatically? Do I need to create a category with a fork for iOS 9 and previous iOS?

like image 531
Valentin Shamardin Avatar asked Sep 21 '15 09:09

Valentin Shamardin


People also ask

What font does iOS 8 use?

When running iOS 7 or iOS 8, all iPhone models use Helvetica Ultra Light or Helvetica Light. When running iOS 9 or later, all iPhone models use a font that is fairly similar to Helvetica, but that Apple calls "San Francisco" and that is shared by the Apple Watch.

What is the system font of iOS?

SF Pro. This neutral, flexible, sans-serif typeface is the system font for iOS, iPad OS, macOS and tvOS. SF Pro features nine weights, variable optical sizes for optimal legibility, four widths, and includes a rounded variant. SF Pro supports over 150 languages across Latin, Greek, and Cyrillic scripts.

What is iOS default font?

The default iOS font is SF, or San Francisco. You can download SF Pro for free.

What font does OSX use?

SF Pro is the system font in macOS.


2 Answers

I've created this extension:

import Foundation
import UIKit

enum SystemFontWeight : String {
    case UltraLight = "HelveticaNeue-UltraLight"
    case Thin = "HelveticaNeue-Thin"
    case Light = "HelveticaNeue-Light"
    case Regular = "HelveticaNeue"
    case Medium = "HelveticaNeue-Medium"
    case Semibold = "Helvetica-Bold"
    case Bold = "HelveticaNeue-Bold"
    case Heavy = "HelveticaNeue-CondensedBold"
    case Black = "HelveticaNeue-CondensedBlack"

    var weightValue:CGFloat? {
        if #available(iOS 8.2, *) {
            switch self {
            case .UltraLight:
                return UIFontWeightUltraLight
            case .Thin:
                return UIFontWeightThin
            case .Light:
                return UIFontWeightLight
            case .Regular:
                return UIFontWeightRegular
            case .Medium:
                return UIFontWeightMedium
            case .Semibold:
                return UIFontWeightSemibold
            case .Bold:
                return UIFontWeightBold
            case .Heavy:
                return UIFontWeightHeavy
            case .Black:
                return UIFontWeightBlack
            }
        } else {
            return nil
        }
    }
}

extension UIFont {
    static func systemFontOfSize(fontSize:CGFloat, weight:SystemFontWeight) -> UIFont {
        if #available(iOS 8.2, *) {
            return UIFont.systemFontOfSize(fontSize, weight: weight.weightValue!)

        } else {
            // Fallback on earlier versions
            return UIFont(name: weight.rawValue, size: fontSize)!
        }
    }
}

Which makes it possible to apply font like this:

myLabel.font = UIFont.systemFontOfSize(14, weight: .Medium)

This will automatically set the correct font for both iOS 8 and iOS 9.

like image 157
Antoine Avatar answered Nov 22 '22 05:11

Antoine


Use + systemFontOfSize:weight:. It's available for iOS 8 and above.

For iOS 7, interface builder settings will work, and for code you will need to create a UIFontDescriptor with the appropriate weight.

like image 26
Léo Natan Avatar answered Nov 22 '22 05:11

Léo Natan