我刚接触Spritekit
并尝试调整SKLabelNode
的宽度,然后根据节点内文字的大小调整字体大小。
一直在查看文档,但似乎找不到类似于UILabel
函数的东西:
UILabel.adjustsFontSizeToFitWidth = true
谢谢。
我刚接触Spritekit
并尝试调整SKLabelNode
的宽度,然后根据节点内文字的大小调整字体大小。
一直在查看文档,但似乎找不到类似于UILabel
函数的东西:
UILabel.adjustsFontSizeToFitWidth = true
谢谢。
我发现了一个不错的函数,但是我不太记得具体在哪里找到它了。
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)
}
这会调整标签的字体大小以完全适合宽度,但您可能想更改函数以在所有侧面添加一些额外的填充。