SKLabelNode文本具有两种不同的字体和颜色.这怎么可能? [英] SKLabelNode text with two different fonts and colour. How is this possible?

查看:312
本文介绍了SKLabelNode文本具有两种不同的字体和颜色.这怎么可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SKLabelNode,它设置为显示得分变量,后跟Highscore变量

I have an SKLabelNode which is set up to show the Score Variable followed by the Highscore variable

scoreLabel.text = "\(score)/\(classicHScoreInt)"

现在,一切都很好,但是我希望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是(如上所述)整数,score

classicHScoreInt is (as stated) an integer and so is score

推荐答案

您不能将两个字体设置为相同的SKLabelNode实例.相反,您可以编写子类来创建一个自定义节点,其中包含多个具有不同字体大小的SKLabelNodes.例如,您的scoreLabel可以是以下类的实例.

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)
    }

}

使用ScoreLabel

let scoreLabel = ScoreLabel()
scoreLabel.position = CGPointMake(100, 300)
scoreLabel.score = 10
self.addChild(scoreLabel)

ScoreLabel中的两个标签从外部充当单个SKNode. 可以在ScoreLabel上执行SKActions,这将同时影响child label nodes.例如

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.

这篇关于SKLabelNode文本具有两种不同的字体和颜色.这怎么可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆