Search code examples
iosswiftfontsuikit

Asking for San Francisco font in different size returns a Times New Roman variant on iOS 13.1


When executing this:

let l = UILabel()
let f = l.font!
let f8 = UIFont(name: f.fontName, size: f.pointSize + 8)!
NSLog("\(f.fontName)")
NSLog("\(f8.fontName)")

The logs output:

.SFUI-Regular
TimesNewRomanPSMT

Why am I not getting a San Francisco variant anymore when I ask for the same font but a little bigger? Is this a bug in iOS?

When I do

let f8 = f.withSize(f.pointSize + 8)

instead I do get .SFUI-Regular.


Solution

  • Use f.familyName not f.fontName

    FamilyName: https://developer.apple.com/documentation/uikit/uifont/1619033-familyname

    FontName: https://developer.apple.com/documentation/uikit/uifont/1619024-fontname

    Also, fontName from init(name:size) must contain full name, including familyName: https://developer.apple.com/documentation/uikit/uifont/1619041-init