Setting background color for SKLabelNode?

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)

be sure to include the text value before calculating the background’ GCSize from the label node, else the values returned will be zero as it has no text to determine the size of the label.

..correct about layer issues


Try adding the SKLabelNode as a child of a SKSpriteNode.

SKLabelNode *label = [[SKLabelNode alloc]initWithFontNamed:@"Helvetica"];
label.position = CGPointMake(0, -label.frame.size.height/2);

SKSpriteNode *background = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(label.frame.size.width, label.frame.size.height)];
background.position = CGPointMake(200, 100);

[background addChild:label];
[self addChild:background];