我在下面的这段文字太长了,无法在屏幕上运行。我如何制作这2行而不是1行?
myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
答案 0 :(得分:2)
您可以创建自己的方法来显示多行文字,如下所示:
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 :(得分:-1)
<强>#1 强>
设置行数:
/web
<强>#2 强>
使用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
希望有所帮助:)
修改强>
您应该澄清您使用的是myLabel.text = "The weather today is going to\nbe partly cloudy with a chance of rain"
而不是正常SKLabelNode
。以下是您可以执行的操作,因为UILabel
不支持多行。
答案 2 :(得分:-2)
myLabel.numberOfLines = 2
//或设置为0 myLabel.frame.size.height
足够