12

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];
Jaffer Sheriff
  • 1,444
  • 13
  • 33

3 Answers3

12

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];
Sudeep
  • 796
  • 6
  • 16
2

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)
Akhilendra Singh
  • 661
  • 11
  • 16
1

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

Gabby
  • 11
  • 4