我有下面的這段文字,它太長,以至於無法放在屏幕上。我怎樣才能製作這兩行而不是一行?如何讓一個標籤節點的文本佔用2行?
myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
我有下面的這段文字,它太長,以至於無法放在屏幕上。我怎樣才能製作這兩行而不是一行?如何讓一個標籤節點的文本佔用2行?
myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
您可以創建自己的方法來顯示多行文本如下:
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
displayMultiLineTextAt(CGRectGetMidX(self.frame) - 100, y: CGRectGetMidY(self.frame) + 50, align: SKLabelHorizontalAlignmentMode.Left, lineHeight: 20.0, text: "Welcome to StackOverFlow!\nThe weather today is going to\nbe partly cloudy with a chance\nof rain.")
}
func displayMultiLineTextAt(x: CGFloat, y: CGFloat, align: SKLabelHorizontalAlignmentMode, lineHeight: CGFloat, text: String) {
let textNode = SKNode()
textNode.position = CGPointMake(x, y)
var lineAt: CGFloat = 0
for line in text.componentsSeparatedByString("\n") {
let labelNode = SKLabelNode(fontNamed: "Arial")
labelNode.fontSize = 14
labelNode.horizontalAlignmentMode = align
labelNode.fontColor = SKColor(hue: 1, saturation: 1, brightness: 1, alpha: 1)
labelNode.position = CGPointMake(0, lineAt)
labelNode.text = line
textNode.addChild(labelNode)
lineAt -= lineHeight
}
scene!.addChild(textNode)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
#1
設置的行數:
myLabel.numberOfLines = 2 //set to 0 if you want it to automatically decide based on how many line breaks you add in step 2
#使用\n
或\r
在文本2
添加換行符:
myLabel.text = "The weather today is going to\nbe partly cloudy with a chance of rain"
希望幫助:)
編輯
你應該澄清的是你用的是SKLabelNode
,而不是一個正常的UILabel
。由於SKLabelNode
s不支持多行,因此以下是您可以執行的操作。
myLabel.numberOfLines = 2
//或設置爲0myLabel.frame.size.height
足夠感謝您的回覆。出於某種原因,它不會讓我做numberOfLines。它給出了錯誤「SKLabelNode沒有成員numberOfLines」 – NickyNick321
SKLabelNode絕對不支持這一點。它與正常的textLabel不同。您必須編寫一些代碼才能使其顯示兩行。 – Vakas
感謝您的答覆。出於某種原因,它不會讓我做numberOfLines。它給出錯誤「SKLabelNode沒有成員numberOfLines」 – NickyNick321
噢好吧'SKLabelNode's似乎是不可能的。你可以做的是創建一個視圖,並添加你的當前和另一個。將一半的文本放在標籤1中,另一半放在標籤2中。將標籤2放置在標籤1下方〜10。這看起來像2行。 – LinusGeffarth