1
votes

Is it currently possible to use iOS 9's new San Francisco font in SpriteKit's SKLabelNode? Apple has explicitly asked us not to instantiate the font by name, but that's the only way SKLabelNode provides for selecting fonts.

2

2 Answers

2
votes

Get the system font name by the property fontName of UIFont:

let myLabel = SKLabelNode(fontNamed:UIFont.systemFontOfSize(45.0).fontName)

Print result of the font name:

.SFUIDisplay-Regular

EDIT:

In terms of WWDC 2015 Video: Introducing the New System Fonts, page 298 of PDF says Don’t Access System Font by Name, e.g.

let myFont = UIFont.systemFontOfSize(mySize)
// …later in the code…
let myOtherFont = UIFont.fontWithName(myFont.familyName size:mySize)

The better practice (page 299) is to Do Reuse Font Descriptors, e.g.

let systemFont = UIFont.systemFontOfSize(mySize)
// …later in the code…
let myOtherFont = UIFont.fontWithDescriptor(systemFont.fontDescriptor()
 size:mySize)

Please note the code is describing a situation that you need to access a font (myFont) that defined earlier. In order to keep consistency for the font in the App, you'd better use fontDescriptor which has already been defined rather than accessing a complete new font without setting its attributes. Don't get confused.

0
votes

This is working well for me (in iOS 14.5):

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

Note that you can also choose from among all the different system-font's weights.