Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting background color for SKLabelNode?

I want to know is there a way to set background color for a SKLabelNode not font color. I'm looking for something like below mentioned code, which is available in ios apps.

     label.backgroundColor = [UIColor redColor];
like image 572
Jaffer Sheriff Avatar asked Apr 24 '15 17:04

Jaffer Sheriff


1 Answers

Adding the SKLabelNode as a child of a SKSpriteNode works but it hides the text. So, I resolved this issue by setting the zPosition of background to a negative number. Here is the swift 3 code:

var label = SKLabelNode(fontNamed: "Helvetica")
label.position = CGPoint(x: CGFloat(0), y: CGFloat(-label.frame.size.height / 2))
var background = SKSpriteNode(color: UIColor.red, size: CGSize(width: CGFloat(label.frame.size.width), height:CGFloat(label.frame.size.height)))background.position = CGPoint(x: CGFloat(200), y: CGFloat(100))
background.zPosition = -1
label.addChild(background)
self.addChild(label)
like image 59
Akhilendra Singh Avatar answered Sep 19 '22 17:09

Akhilendra Singh