Centre a SKLabelNode on a SKSpriteNode

寵の児 提交于 2019-12-04 02:55:10

I realized how to solve this...here's what i did. Keep in mind that I have a class called Button that is a subclass of SKSpriteNode.

In the Button.m class I have an instance variable called label that is a SKLabelNode. I add the label node as a child to the button then set the horizontal and vertical alignment modes to centre.

label = [[SKLabelNode alloc] init];
[self addChild:label];
[label setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];
[label setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];
guest

This will put your label in center of scene in sprite kit:

yourLabel.horizontalAlignmentMode = .Center;
yourLabel.verticalAlignmentMode = .Center

Swift 4.2 XCode 10.1

Copy this function into your SceneKit Class

func createLabel(text: String) {
        let label = SKLabelNode(fontNamed: "Wicked Mouse")

        label.text = text
        label.fontColor = .white
        label.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
        label.verticalAlignmentMode = .center
        label.horizontalAlignmentMode = .center
        label.fontSize = 30.0
        label.zPosition = 1
        self.addChild(label)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!