【发布时间】:2017-06-13 00:26:18
【问题描述】:
我正在尝试以特定大小将一些文本置于场景中心。
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)
}
所以我使用了adjustLabelFontSizeToFitRect:
adjustLabelFontSizeToFitRect(labelNode: price, rect: CGRect(origin: CGPoint(x: 0, y: 0), size : buttonSize))
将 SKNodeLabel 的大小调整为特定大小。但我希望那个标签的原点是屏幕的中心,所以 (0,0),因为锚点是 0.5、0.5。但是,我明白了:
adjustLabelFontSizeToFitRect() 是这样的:
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)
}
【问题讨论】:
标签: swift sprite-kit sklabelnode