iosswiftsprite-kitsklabelnode

Adding a background to a SKLabelNode


I have been trying to add a background around my SKLabelNode, but I can't figure out how to do it programmatically in swift 3. I see some old posts that use .background, but that doesn't work for me.

let background = SKSpriteNode(color: UIColor.white, size: CGSize(width: CGFloat((title.frame.size.width)), height:CGFloat((title.frame.size.height)))) 
background.zPosition = -1
background.position = CGPoint(x: self.frame.width / 12 * 6, y: self.frame.height / 12 * 11); title.addChild(background)

Solution

  • Check this new code, I think this suited more to your problem

        let shape = SKShapeNode()
        shape.path = UIBezierPath(roundedRect: CGRect(x:(label?.frame.origin.x)! - 15, y: (label?.frame.origin.y)! - 15, width: ((label?.frame.size.width)!+30), height: ((label?.frame.size.height)! + 50 )), cornerRadius: 64).cgPath
        shape.position = CGPoint(x: frame.midX, y: frame.midY)
        shape.fillColor = UIColor.red
        shape.strokeColor = UIColor.blue
        shape.lineWidth = 5
        label?.addChild(shape)
    

    Layout