调整文本大小以适合具有固定宽度的SKLabelNode

dan*_*ush 9 sprite-kit sklabelnode swift2

新增Spritekit并尝试修复a的宽度,SKLabelNode然后相应地调整文本字体以适合节点.

一直在浏览文档,但似乎找不到合适的东西,比如UILabel功能:

一个 UILabel.adjustsFontSizeToFitWidth = true

谢谢

Aid*_*ser 7

这是我不久前发现的一个不错的功能,但我不记得确切位置

    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)
}
Run Code Online (Sandbox Code Playgroud)

这会调整标签的字体大小以完全适合宽度,但您可能需要更改该功能以在所有侧面添加一些额外的填充。