SKLabelNode - 如何在多条线路中心对齐?

问题描述 投票:6回答:3

使用SKLabelNode,当你突破多行时,

结果总是如此

abcde
fg

而不是

abcde
 fg

真的,似乎SKLabelNode只是左对齐,就是这样。

有没有解决方案 - 如何使多线SKLabelNode居中对齐?


注意 - horizo​​ntalAlignmentMode完全不相关。它只是允许您选择整个标签区域的锚点是位于位置的左侧,右侧还是中心。

ios sprite-kit sknode
3个回答
9
投票

我通过将attributionText设置为SKLabelNode来解决了这个问题。看起来像最简单的方法,希望Apple尽快解决这个问题。

斯威夫特4

let attrString = NSMutableAttributedString(string: yourString)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let range = NSRange(location: 0, length: yourString.count)
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: finalRange)
attrString.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.black, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 30)], range: range)
yourLabelNode.attributedText = attrString

2
投票

似乎Apple的SKLabel根本不是多行文本的中心。 (截至2018年。)

这种行为只能被描述为破碎 - 无论在什么设置下,第二行和后一行都会向左冲洗。

一种解决方案是使用“第二标签”,以下示例。

Later...

@MartinŠkorc似乎发现你可以使用AttributedString和SKLabels - 太棒了!谢谢马丁!

import SpriteKit

class StupidMultilineSKLabelNode: SKLabelNode {

    // Apple's SKLabel does not center multi-line text

    // this class works around the problem by making
    // a "sub" SKLabel for the second line

    // if the .text includes a newline, a line break,
    // this class will present it as a two-line SKLabel,
    // but properly centered (each line itself centered).
    // (this example allows just the one extra line)

    // please note,
    // this is not meant to be a generalized class;
    // rather it is meant to work quick in a specific case.
    // you can change it as needed

    // using a "second label" does seem to be the only solution
    // until Apple address the issue

    var stupidExtraLabel: SKLabelNode

    override init() {

        stupidExtraLabel = SKLabelNode()
        super.init()

        stupidExtraLabel.basicSettings()
        // the simplest approach: in "basicSettings" simply set
        // your point size, font, color, etc etc as you wish
        // just always use that call to set all labels in the project

        stupidExtraLabel.position = CGPoint(x: 0, y: -10)
        stupidExtraLabel.text = ""

        self.addChild(stupidExtraLabel)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var alpha: CGFloat {

        didSet {

            super.alpha = alpha
            stupidExtraLabel.alpha = alpha
        }
    }

    override var fontColor: UIColor? {

        didSet {

            super.fontColor = fontColor
            stupidExtraLabel.fontColor = fontColor
        }
    }

    override var text: String? {

        didSet {

            let lines: [String] = text!.components(separatedBy: ["\n"])
            super.text = ""
            stupidExtraLabel.text = ""
            if lines.count > 0 { super.text = lines[0] }
            if lines.count > 1 { stupidExtraLabel.text = lines[1] }

            stupidExtraLabel.position = CGPoint(x: 0, y: -10)
        }
    }
}

1
投票

跟进 - 我发布了一个链接而不是代码,这是一种禁忌。我没有大代表(noob),所以我也无法编辑:)

我最近发现你可以将UIKit叠加在scenekit之上(也可能是Sprite Kit)。我已经尝试了它并且它在FPS中没有任何损失,所以我删除了所有的Spritekit覆盖(菜单/标签)和使用UIKit基础知识(UIButton,UILabel)。如果有兴趣,我可以分享一些代码。我还在搞清楚这些事情,但是我觉得它会发挥作用。

© www.soinside.com 2019 - 2024. All rights reserved.