2015-08-21 171 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

相關問題