2015-08-21 44 views
6

我在sprite工具包中创建一个标签并设置初始大小。由于应用程序要进行本地化,所以其他语言的文字可能会比英文版本更长。因此,如何调整标签的字体大小以适应特定的宽度,在这种情况下是按钮。调整SKLabelNode字体大小以适合?

myLabel = SKLabelNode(fontNamed: "Arial") 
myLabel.text = "Drag this label" 
myLabel.fontSize = 20 
+1

这个类似的问题应该可以帮到你我认为>> http://stackoverflow.com/q/30980918 –

回答

4

我能够解决这个由于@InvalidMemory的评论和@ mike663的答案。基本上,您可以按照包含标签的矩形比例缩放标签。

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) 
} 

这里是链接到other question

相关问题