如何让一个label节点的文本占2行?
How do I make the text of one label node take up 2 lines?
我下面的这段文字太长,无法在运行时显示在屏幕上。我怎样才能使这两条线而不是一条线?
myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
#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
#2
使用 \n
或 \r
:
在文本中添加换行符
myLabel.text = "The weather today is going to\nbe partly cloudy with a chance of rain"
希望对您有所帮助:)
编辑
您应该澄清您使用的是 SKLabelNode
而不是普通的 UILabel
。这是您可以执行的操作,因为 SKLabelNode
s 不支持多行。
- 创建新视图
- 向视图添加标签 #1
- 向视图添加另一个标签 (#2),将其放在标签 #1 下方
- 将句子分成两部分,将第一部分添加到标签 #1,第二部分添加到标签 #2
myLabel.numberOfLines = 2
//或设置为0
- 确保
myLabel.frame.size.height
足够
edit/update:
** 对于 iOS 11 或更高版本,您可以设置 numberOfLines, lineBreakMode, and preferredMaxLayoutWidth properties. attributedText 也受支持 **
原回答
您可以创建自己的方法来显示多行文本,如下所示:
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 Whosebug!\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 */
}
}
我下面的这段文字太长,无法在运行时显示在屏幕上。我怎样才能使这两条线而不是一条线?
myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
#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
#2
使用 \n
或 \r
:
myLabel.text = "The weather today is going to\nbe partly cloudy with a chance of rain"
希望对您有所帮助:)
编辑
您应该澄清您使用的是 SKLabelNode
而不是普通的 UILabel
。这是您可以执行的操作,因为 SKLabelNode
s 不支持多行。
- 创建新视图
- 向视图添加标签 #1
- 向视图添加另一个标签 (#2),将其放在标签 #1 下方
- 将句子分成两部分,将第一部分添加到标签 #1,第二部分添加到标签 #2
myLabel.numberOfLines = 2
//或设置为0- 确保
myLabel.frame.size.height
足够
edit/update:
** 对于 iOS 11 或更高版本,您可以设置 numberOfLines, lineBreakMode, and preferredMaxLayoutWidth properties. attributedText 也受支持 **
原回答
您可以创建自己的方法来显示多行文本,如下所示:
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 Whosebug!\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 */
}
}