I have an SKLabelNode
which is set up to show the Score Variable followed by the Highscore variable
scoreLabel.text = "\(score)/\(classicHScoreInt)"
Now, everything shows fine but i would like the classicHScoreInt to be a smaller font and maybe a different colour. How is this possible?
classicHScoreInt
is (as stated) an integer and so is score
You cannot set two fonts to the same SKLabelNode
instance. Instead you can write subclasses to create a custom node which contains multiple SKLabelNodes
with different font sizes. For example, Your scoreLabel can be an instance of the following class.
class ScoreLabel : SKNode
{
var label : SKLabelNode!
var scoreLabel : SKLabelNode!
var score : Int = 0 {
didSet
{
scoreLabel.text = "\(score)"
}
}
override init() {
super.init()
label = SKLabelNode(text: "Score : ")
label.position = CGPointMake(0, 0)
label.fontSize = 20
addChild(label)
scoreLabel = SKLabelNode(text: "\(0)")
scoreLabel.position = CGPointMake(label.frame.size.width , 0)
scoreLabel.fontSize = 25
addChild(scoreLabel)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Using ScoreLabel
class
let scoreLabel = ScoreLabel()
scoreLabel.position = CGPointMake(100, 300)
scoreLabel.score = 10
self.addChild(scoreLabel)
The two labels in ScoreLabel
acts as a single SKNode
from the outside.
SKActions
can be executed on the ScoreLabel
and it will affect both the child label nodes
. For instance
scoreLabel.runAction(SKAction.scaleTo(2.0, duration: 2.0))
This will scale both labels together as a single unit.
来源:https://stackoverflow.com/questions/28356256/sklabelnode-text-with-two-different-fonts-and-colour-how-is-this-possible