I am trying to get center some text at the center of the scene with a specific size.
func addCubeExperienceLabel(_ buttonSize : CGSize, _ buttonPos : CGPoint, _ world : Int) {
let price = SKLabelNode(fontNamed: "LCDSolid")
price.text = String(WorldPrices.fromInt(world).rawValue)
price.zPosition = 50
adjustLabelFontSizeToFitRect(labelNode: price, rect: CGRect(origin: CGPoint(x: 0, y: 0), size : buttonSize))
self.addChild(price)
}
So I use the adjustLabelFontSizeToFitRect:
adjustLabelFontSizeToFitRect(labelNode: price, rect: CGRect(origin: CGPoint(x: 0, y: 0), size : buttonSize))
To adjust the size of the SKNodeLabel to a specific size. But I want the origin of that label to be the center of the screen, so (0,0), since the anchor point is 0.5, 0.5. However, I get this:
The adjustLabelFontSizeToFitRect() is this:
func adjustLabelFontSizeToFitRect(labelNode:SKLabelNode, rect:CGRect) {
// Determine the font scaling factor that should let the label text fit in the given rectangle.
let scalingFactor = min(rect.width / labelNode.frame.width, rect.height / labelNode.frame.height)
// Change the fontSize.
labelNode.fontSize *= scalingFactor
// Optionally move the SKLabelNode to the center of the rectangle.
labelNode.position = CGPoint(x: rect.midX, y: rect.midY - labelNode.frame.height / 2.0)
}