Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monospaced system font replaced by Times New Roman in iOS 13+ #92

Open
jeffjfk opened this issue Aug 10, 2020 · 1 comment
Open

Monospaced system font replaced by Times New Roman in iOS 13+ #92

jeffjfk opened this issue Aug 10, 2020 · 1 comment

Comments

@jeffjfk
Copy link

jeffjfk commented Aug 10, 2020

Hello there,
First of all, thanks a lot for this library, soooo usefull :)

I'm using it in my iOS app and it works very well, even with custom fonts!

But I think I found a bug when using a monospaced system font, running on iOS 13:
in some cases, I need to use UIFont.monospacedDigitSystemFont(ofSize: 30, weight: .bold).
So, instead of using a custom font, I use a system one.
Oddly, this causes my label to be displayed with Times New Roman while I'm not using this font at all.

I found this radar which could explain what's going on: https://openradar.appspot.com/6153065

Back in the code, looking for the what line is responsible of this change...
Here is my code to set each needed styles:

let md = KwitMarkdown(string: myText)
md.setFontColorForAllStyles(with: myColor)
md.setFontSizeForAllStyles(with: myFont.pointSize)
md.setFontNameForAllStyles(with: myFont.fontName)
			
let boldFont      = kwitEmphasizedFont
md.bold.fontName  = myBoldFont.fontName
md.bold.fontSize  = myBoldFont.pointSize
md.bold.color     = myBoldColor ?? myColor

It works well for every texts in a custom font (I'm using Gilroy in my app) except for those with a system one.
It appears the font change occurs in the file SwiftyMarkdown+iOS.swift.

func font( for line : SwiftyLine, characterOverride : CharacterStyle? = nil ) -> UIFont {

//before this condition, 
//font == <UICTFont: 0x7f9dbd61fc10> font-family: "UICTFontTextStyleBody"; font-weight: normal; font-style: normal; font-size: 17.00pt
//finalSize == 30.0
//textStyle == UICTFontTextStyleBody
//existentFontName == ".SFUI-Bold"
//It seems OK

    if let customFont = UIFont(name: existentFontName, size: finalSize)  {
        //but starting here, 
        //customFont == <UICTFont: 0x7f9dbb4bf650> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 30.00pt
        ...
    }
...

Looking at this WWWDC2019 video, at 2'45 it is said we should no more try to get a UIFont by it's name : https://developer.apple.com/videos/play/wwdc2019/227/

Has anyone any idead how to solve that?

@jeffjfk jeffjfk changed the title Monospaced system font replaced by Times New Roman Monospaced system font replaced by Times New Roman in iOS 13+ Aug 10, 2020
@erychagov
Copy link

I made some workaround, hope it will be helpfully:

let markdown = SwiftyMarkdown(string: text)
let mutableAttributedText = NSMutableAttributedString(attributedString:  markdown.attributedString())
mutableAttributedText.enumerateAttributes(
    in: NSRange(location: 0, length: mutableAttributedText.length),
    options: NSAttributedString.EnumerationOptions.reverse
) { (attrs, range, _) in
    guard attrs.keys.contains(NSAttributedString.Key.font) else {
        return
    }
    mutableAttributedText.removeAttribute(NSAttributedString.Key.font, range: range)
    mutableAttributedText.addAttribute(NSAttributedString.Key.font, value: font, range: range)
}

The idea is just to replace all font attributes by your font

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants