本文介绍了调整SKLabelNode字体大小以适合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Sprite Kit内创建标签并设置初始大小.由于该应用程序将进行本地化,因此其他语言中的单词可能会比英语版本更长.因此,如何调整标签的字体大小以适合某个宽度(在这种情况下为按钮).
I'm creating a label inside of sprite kit and setting an initial size. Since the app is to be localized, words may appear longer in other languages than their english version. Therefore how can I adjust the font size of the label to fit within a certain width which in this case is the button.
myLabel = SKLabelNode(fontNamed: "Arial")
myLabel.text = "Drag this label"
myLabel.fontSize = 20
推荐答案
由于@InvalidMemory的评论和@ mike663的回答,我得以解决此问题.基本上,您可以根据包含标签的矩形按比例缩放标签.
I was able to solve this thanks to a comment by @InvalidMemory and the answer by @mike663. Basically you scale the label in proportion to the rectangle that contains the label.
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)
}
此处是.
这篇关于调整SKLabelNode字体大小以适合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!