Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the system font of iOS13?

Tags:

ios

swift

fonts

What is the iOS 13 system font?

Before iOS 13 I used SFUIDisplay font.

UIFont(name: ".SFUIDisplay-Light", size: UIFont.systemFontSize)

But on iOS 13 it doesn't work.

like image 741
Vladislav Avatar asked Sep 03 '19 06:09

Vladislav


2 Answers

This bug is so BS. The only way to get around it is by using the systemFont(ofSize:weight:) method directly. AKA, you cannot get the system font using the method UIFont(name:size:), you ll just get Times New Roman for some funny reason. Apple devs must be messing with us. So for the original question above you must use the following:

UIFont(systemFont:UIFont.systemFontSize, weight: .light)

For my situation, I ran into this bug making an extension for UIFont to return the same font in a different size, which must work with custom and system fonts. In order for it to work on xcode 11 ios 13, I had to add a silly check to see if fontName contains ".SF".

extension UIFont {
    func forSize(_ pointSize: CGFloat) -> UIFont {
        if !fontName.contains(".SF"), let font = UIFont(name: fontName, size: pointSize) {
            return font
        }

        return UIFont.systemFont(ofSize: pointSize, weight: weight)
    }
}
like image 184
Lucas Chwe Avatar answered Sep 21 '22 14:09

Lucas Chwe


If you are aiming to use the system font, you don't really have to worry about its name, you should let the system to do it for you.

let font = UIFont.systemFont(ofSize: UIFont.systemFontSize)

At this point, whenever the system font changes, it will automatically updated.

Moreover

I use a lot of custom fonts. I need to do it

Actually, you could do it without mentioning the font name in case you want to use the system font. For example, you could implement a function that returns the proper font as:

func getFont(name: String = "", size: CGFloat = UIFont.systemFontSize) -> UIFont {
    // system font
    let defaultFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)

    if name.isEmpty {
        return defaultFont
    }

    return UIFont(name: name, size: size) ?? defaultFont
}

For using the system font, call it: getFont(). Otherwise, call it with mentioning the name of the font: getFont(name: ".SFUIDisplay-Light").

However, you might think of doing something like this to get the system font name and then use it:

let systemFontName = UIFont.systemFont(ofSize: UIFont.systemFontSize).fontName
getFont(name: systemFontName)

I'd say it's meaningless since the UIFont.systemFont automatically detects the system font name without the need of mentioning it.

like image 25
Ahmad F Avatar answered Sep 21 '22 14:09

Ahmad F