0

我在下面的文本太长,运行时无法在屏幕上显示。我怎样才能使这两条线而不是一条线?

myLabel.text = "The weather today is going to be partly cloudy with a chance of rain"
4

3 回答 3

2

编辑/更新:

**对于 iOS 11 或更高版本,您可以设置numberOfLineslineBreakModepreferredMaxLayoutWidth属性。也支持属性文本 **

原始答案


您可以创建自己的方法来显示多行文本,如下所示:

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 */
    }
}
于 2015-06-19T04:27:03.203 回答
-1

#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使用or
在文本中添加换行符:\n\r

myLabel.text = "The weather today is going to\nbe partly cloudy with a chance of rain"

希望有帮助:)

编辑

您应该澄清您使用的是 aSKLabelNode而不是 normal UILabel。这是你可以做的,因为SKLabelNodes 不支持多行。

  • 创建一个新视图
  • 将标签 #1 添加到视图
  • 将另一个标签 (#2) 添加到视图中,将其放在标签 #1 下方
  • 将句子分成两部分,将第一部分添加到标签#1,第二部分添加到标签#2
于 2015-06-19T02:58:03.107 回答
-2
  • myLabel.numberOfLines = 2//或设置为0
  • 确保myLabel.frame.size.height足够
于 2015-06-19T03:08:02.917 回答