SimonFairbairn / SwiftyMarkdown

Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.
MIT License
1.64k stars 275 forks source link

Font defaults to TimesNewRoman in iOS 13 #64

Closed verstaen closed 4 years ago

verstaen commented 5 years ago

Starting with iOS 13, the system default font can't be accessed using fontName. This breaks the default font used on device.

More precisely, this code:

    let font = UIFont.systemFont(ofSize: 12.0)
    let newFont = UIFont(name: font.fontName, size: 12.0)
    print(newFont?.fontName)

prints the expected .SFUIText on Simulator and on device with iOS 12, but TimesNewRomanPSMT on device with iOS 13.

As a consequence, in attributedStringFromString(), when attempting to load the default font (stored through its name, .SFUIText), iOS 13 returns TimesNewRoman...

We could either special case .SFUIText (not my favorite, this will break if Apple modifies this name for any reason), or handle the "system default" font case. I'd be happy to work on a PR.

ayoy commented 5 years ago

I stumbled upon this issue while migrating to iOS 13, and prepared a workaround in my fork. It changes FontProperties API to take a ready-made UIFont object, instead of name and size. It's not thoroughly tested but it does the job for me.

Have a look at this commit if you like.

verstaen commented 5 years ago

Thanks @ayoy , this is very helpful. I worked around this by checking after instantiating UIFont if the resulting font name is the expected one, if not then use the system font and not a random one. I'll work on a PR. I believe both these fixes can be useful.