Search code examples
iosxcodeswiftsprite-kitsklabelnode

SKLabelNode upside down possible?


I'm developing a game in xcode 6 with swift. I have a scorelabel var scoreLabel: SKLabelNode!. Now in one of my methods I show my label:

scoreLabel = SKlabelNode(fontNamed: "TrebuchetMS-Bold")
scoreLabel.text = "\(score)"
scoreLabel.fontSize = 30
scoreLabel.fontColor = SKColor .redColor()
scoreLabel.position = CGPoint(x:780, y:180)
addChild(scoreLabel)

That shows my score as for example: 2500. Is it possible to show this upside down?

  • 2
  • 5
  • 0
  • 0

Sorry, I wrote this as list, because I wasn't able to write upside down here.

Thanks for answers.


Solution

  • You can create a custom class to loop through each character in the string and create a separate SKLabelNode for each character.

    class SKVerticalLabelNode : SKNode
    {
        var fontName : String = "" {
            didSet {
                self.updateLabelProperties()
            }
        }
    
        var fontSize : CGFloat = 10.0 {
            didSet {
                self.updateLabelProperties()
            }
        }
    
        var fontColor : UIColor = UIColor.whiteColor() {
            didSet {
                self.updateLabelProperties()
            }
        }
    
        var text : String = "" {
            didSet {
               self.updateLabelProperties()
            }
        }
    
        func updateLabelProperties () {
            self.removeAllChildren()
            let length = countElements(text)
            var yPosition : CGFloat = 0
            for character in reverse(text) {
                let label = SKLabelNode(fontNamed: self.fontName)
                label.fontColor = self.fontColor
                label.fontSize = self.fontSize
                label.text = String(character)
                label.position = CGPointMake(0, yPosition)
                yPosition += label.frame.size.height
                addChild(label)
            }
        }
    }
    

    It can be used like this

    let scoreLabel = SKVerticalLabelNode()
    scoreLabel.fontName = "TrebuchetMS-Bold"
    scoreLabel.text = "2500"
    scoreLabel.fontSize = 30
    scoreLabel.fontColor = UIColor.redColor()
    scoreLabel.position = CGPoint(x:180, y:180)
    addChild(scoreLabel)