调整 SKLabelNode 字体大小以适应? [英] Resize a SKLabelNode font size to fit?

查看:34
本文介绍了调整 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 字体大小以适应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆