-2

I have a strange problem with iOS 13 system font. I am trying to load iOS default font San Francisco using the font name. But due to some iOS bug it's loading some other font like Times New Roman. I saw few problems like this here and here. But in my case I have to load the font with name because I am using a Utility class to globally set font name in app delegate. Is there any way to load the system font using font name in iOS 13?

// Set font here
 MUIConstats.shared.defaultFontRegularName = UIFont.systemFont(ofSize: 17).fontName


 // Get font from here

 class func appFontRegularOf(size: CGFloat)-> UIFont{


        if let fontName = MUIConstats.shared.defaultFontRegularName {
            return UIFont(name:fontName, size: size) ?? UIFont.systemFont(ofSize:size)
        }else{
            return  UIFont.systemFont(ofSize:size)
        }

    }
Maneesh M
  • 193
  • 1
  • 10
  • 2
    Do not use or store the system font name. Refer to it only as the system font. The whole idea that you are going to do this by storing a simple string name in `MUIConstats.shared.defaultFontRegularName` is wrong. – matt Nov 05 '19 at 11:32
  • Thanks for the reply, may I know why it's wrong? I am using this approach for various reasons. MUI is actually a framework I created. If I am using custom font I need to add the font file into the framework. If I am using font instead of font name I cannot set it from app delegate. – Maneesh M Nov 05 '19 at 12:46
  • 1
    You don't use the name of the system font because it's not supported, as you yourself have discovered. Apple explicitly forbids this. I didn't say you had to use an actual font object; I said you couldn't use a simple name if one of the possibilities is the system font. – matt Nov 05 '19 at 12:52
  • "You don't use the name of the system font", it means I can use this approach if I am not using system font right? – Maneesh M Nov 05 '19 at 13:01
  • So it's not a bug! Is there any reference from apple regarding this? – Maneesh M Nov 05 '19 at 13:13
  • 1
    The bug is assuming it's a bug. You were always doing something wrong; now you're busted. Apple is very clear about this (watch any of the WWDC videos on this topic). – matt Nov 05 '19 at 13:21
  • Why my question is down voted, I am no longer able to add new questions – Maneesh M Dec 26 '19 at 08:37

1 Answers1

1

You can store the name of a nonsystem font, but if it's the system font the only thing you're allowed to store is the fact that it's the system font. This is a perfect opportunity for a union:

enum WhatFont {
    case system
    case name(String)
    func fontOfSize(_ sz:CGFloat) -> UIFont? {
        switch self {
        case .system: return UIFont.systemFont(ofSize: sz)
        case .name(let name): return UIFont(name: name, size: sz)
        }
    }
}

Test it:

let f1 = WhatFont.system
print(f1.fontOfSize(17))
let f2 = WhatFont.name("Georgia")
print(f2.fontOfSize(17))
matt
  • 515,959
  • 87
  • 875
  • 1,141