1

Problem:

The text string to be displayed by an SKLabelNode does not show the leading spaces when shown in the SKScene.

Example (Swift):

class ExampleScene: SKScene {
    var titleLabel = SKLabelNode()

    override func didMove(to view: SKView) {
        titleLabel.fontName = "Menlo"
        titleLabel.fontSize = 40.0
        titleLabel.fontColor = UIColor.white
        titleLabel.position = CGPoint(x: 10.0, y: frame.maxY - 100.0)
        titleLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left
        titleLabel.text = "          Hello World!"
        addChild(titleLabel)
    }
}

Result:

Rather than being displaye in the SKScene as:

" Hello World!"

It is displayed as:

"Hello World!"

Why do I need this?

I would like the user to type out the string being displayed, similar to those games where one learns how to type properly. With every successive character typed correctly, String.removeFirst() is called to "pop" the first character off the string variable and this new string is displayed by the SKLabelNode.

In the case of "Hello World!", if the string gets to " World!", it shows up as "World!" and the user will not have the visual cue that he/she is supposed to type a space, instead of the "W" character.

What troubleshooting did I do?

  • I found a similar thread from years ago, but the responses did not address my specific issue and the op did not reply back: SKLabelNode removes leading and trailing spaces - How can I stop that?
  • I've also tried different fonts, both fixed and variable length such as Menlo and AvenirNext-Bold, but it's not a font issue.
  • Changing font alignment via the SKLabelNode horizontalAlignmentMode property to anything other than left doesn't make sense either, as I'd like the string to be typed always up against the left margin. Searching
  • Apple's documentation doesn't yield anything regarding this default behaviour.

Help will be greatly appreciated.

  • did you change the linebreakmode? – Knight0fDragon Jul 09 '18 at 16:05
  • Per your suggestion, I've tried titleLabel.lineBreakMode = .byTruncatingHead to see whether there was any indication of truncation via an ellipsis glyth but none was shown. I should add that even though the string to be displayed could be several 1000 characters, the actual string being fed into SKLabelNode is a substring which fits all on one line via the String.prefix() method. –  Jul 09 '18 at 16:24
  • ok, well I am going to guess that SKLabelNode is framing around visible pixels, so you are going to have to use some kind of alternative, like having your text right aligned and then doing some math on your own to center it – Knight0fDragon Jul 09 '18 at 18:22
  • As a temporary solution, I've created an alternative UILabel with essentially the same properties as the existing SKLabelNode. UILabel doesn't have this problem and displays any leading space characters correctly (i.e. whitespace). Still, I would prefer using SKSpriteNode if possible. UILabel is currently added to the UIView, instead of the SKScene and has the .isHidden properties toggled to show/hide depending on which SKScene is showing. From a design perspective, I would've thought keeping everything as SpriteKit objects would be easier. –  Jul 09 '18 at 21:26
  • Well you have a couple of options, another one being you convert the text of the font to a CGPath, then use SKShapeNode. Of course the CGPath is also not going to recogize the space most liklely, but you will have the option of translating the path before assigning it to the SKShapeNode – Knight0fDragon Jul 10 '18 at 12:30

0 Answers0