System Font Not Working on SKLabelNodes in iOS13

I have a label in a SpriteKit app


var speedLabel: SKLabelNode = SKLabelNode()


And when I try to assign it a Font like so:


speedLabel.fontName = UIFont.preferredFont(forTextStyle: .body).fontName


In iOS 11.0-12.x, that returns ".SFUIText" and gives the label the proper san-serif system font.


In iOS 13.0, it returns ".SFUI-Regular" and shows up as some serifed, maybe Times New Roman font.


I remember having issues with SpriteKit Label Nodes and fonts in the past. If you assigned it an invalid font, it would default to this serifed font.


Anyone have any ideas how to fix this?

Accepted Reply

I faced the same problem and found that it seems SKLabelNode’s attributedText correctly recognizes “.SFUI-Regular”. The code below displays the san-serif font.


label.attributedText = NSAttributedString(string: "blah-blah-blah", attributes: [.font: UIFont.preferredFont(forTextStyle: .body)])

Replies

I faced the same problem and found that it seems SKLabelNode’s attributedText correctly recognizes “.SFUI-Regular”. The code below displays the san-serif font.


label.attributedText = NSAttributedString(string: "blah-blah-blah", attributes: [.font: UIFont.preferredFont(forTextStyle: .body)])

Thank you so much for replying! I wonder why it changed in iOS 13. 😠

I used

SKLabelNode(fontNamed: "SFUI-Regular")

instead.

I'm facing an issue where I'm unable to use the 'Heavy' or 'Bold' system fonts on a SpriteKit SKLabelNode. When I do what you've suggested and use the string "SFUI-Semibold" etc, the displayed font is still just SFUI-Regular.


Edit:

I have reported this to apple as a bug.

You can also choose from among all the different system-font's weights via the attributedText approach. For instance, this effectively renders my text bold for this label-node (in iOS 14.5):

SKLabelNode(
  attributedText: NSAttributedString(
    string: text,
    attributes: [.font: UIFont.systemFont(ofSize: 16, weight: .bold)]
  )
)