swiftsprite-kitsklabelnode

SKLabelNode.color vs SKLabelNode.fontColor


What is the difference between SKLabelNode.color and SKLabelNode.fontColor? I already know how to use SKLabelNode, and I do not want any answers stating how to use the SKLabelNode. Any help would be appreciated.


Solution

  • The documentation explains what they are:

        /**
         Base color that the text is rendered with (if supported by the font)
         */
        open var fontColor: UIColor?
    
    
        /**
         Controls the blending between the rendered text and a color. The valid interval of values is from 0.0 up to and including 1.0. A value above or below that interval is clamped to the minimum (0.0) if below or the maximum (1.0) if above.
         */
        open var colorBlendFactor: CGFloat
    
    
        /**
         Color to be blended with the text based on the colorBlendFactor
         */
        open var color: UIColor?
    

    Here is an example to help better understand how it works:

        let label1 = SKLabelNode(text: "Red")
        label1.position = CGPoint(x: 0, y: 50)
        label1.fontColor = .red
        addChild(label1)
    
        let label2 = SKLabelNode(text: "Red")
        label2.color = .red
        label2.colorBlendFactor = 1.0
        addChild(label2)
    
        let label3 = SKLabelNode(text: "Dull Red")
        label3.position = CGPoint(x: 0, y: -50)
        label3.color = .red
        label3.colorBlendFactor = 0.5
        addChild(label3)
    
        let label4 = SKLabelNode(text: "White")
        label4.position = CGPoint(x: 0, y: -100)
        label4.color = .red
        label4.colorBlendFactor = 0.0
        addChild(label4)
    

    By default all labels are white which comes into play when using colorBlendFactor. label1 and label2 are the same red color. label1 is red because it's font color is set to red. label2 is red because red is blending with its white color at a factor of 1.0. As the label's colorBlendFactor nears 0 it'll get more and more white as seen in label3 and label4.