Damiano Miazzi
Damiano Miazzi

Reputation: 2325

SKLabelNode, can't use "SFMono-Light" as font

im try to use the font "SFMono-Light" as font of my SKlabel.. but keep getting the error says "SKLabelNode: SFMono-Light" font not found.


  func loadSKscene()->SKScene{ // load screenPFD
        let loadedScene = SKScene(fileNamed: "/Asset.scnassets/Korry.sks")!
        loadedScene.scaleMode = .resizeFill
        let skLabel = SKLabelNode(text: "ON")
        skLabel.fontColor = .white
        skLabel.fontSize = 35
        skLabel.fontName = "SFMono-Light"
        skLabel.horizontalAlignmentMode = .center
        skLabel.verticalAlignmentMode = .center
        skLabel.position = CGPoint(x: 0, y: 29)
        skLabel.zPosition = 1
        skLabel.name = "firstLineText"
        loadedScene.addChild(skLabel)
        
        //Test to find the children font
        print(loadedScene.children)
        

        return loadedScene
    }

Funny story, if from the editor I manually insert a SKlabelNode with font SFMono that's work fine ...

See the print out of my test:

pic

what I'm doing wrong..? why from editor I can set the Font and from code I can't.. Thanks

Upvotes: 0

Views: 106

Answers (1)

Ranoiaetep
Ranoiaetep

Reputation: 6637

First, for both the SF and SF Mono font, you must refer them by the family name, SF and SF Mono. And you can not add the weight suffix to them.

However that means you can't set the font weight. Instead, you could use an AttributeString for SKLabelNode:

let skLabel = SKLabelNode()
skLabel.attributedText = .init(
    string: "Score: 0", 
    attributes: [
        .font: NSFont.monospacedSystemFont(ofSize: 35, weight: .light)
    ])

Upvotes: 1

Related Questions