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.