Ich versuche, einen Text in der Mitte der Szene mit einer bestimmten Größe zu zentrieren.Ursprungspunkt eines CGRect in SpriteKit
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 verwende ich die adjustLabelFontSizeToFitRect:
adjustLabelFontSizeToFitRect(labelNode: price, rect: CGRect(origin: CGPoint(x: 0, y: 0), size : buttonSize))
Um die Größe des SKNodeLabel auf eine bestimmte Größe anpassen. Aber ich möchte, dass der Ursprung dieses Etiketts der Mittelpunkt des Bildschirms ist, also (0,0), da der Ankerpunkt 0,5, 0,5 ist. Allerdings bekomme ich diese:
Die adjustLabelFontSizeToFitRect() ist dies:
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)
}
Es hat sich überhaupt nichts geändert – Pablo