SKLabelNode removes leading and trailing spaces. How can I stop this?

I wanted to create an SKLabelNode that should always be the same length (for a word guessing game). Unfortunately, SKLabelNode decides to always turn off any leading and trailing spaces. This behavior is not described in the documentation. How can I avoid / disable this behavior?

+7
ios sprite-kit sklabelnode
source share
1 answer

If you just want the labels aligned so that they are right aligned, use the right alignment mode.

myLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight; 

However, if you try to create a rectangle around SKLabelNode, you will find that calcAccumulatedFrame does separate the spaces before calculating the label width and returns the wrong size.

But you can fool - use the symbol as a label for the size of the layout to add the size of your frame.

 SKLabelNode* dummyPaddingLetter = [SKLabelNode labelNodeWithFontNamed:<your font name>]; dummyPaddingLetter.fontSize = <your font size>; dummyPaddingLetter.text = @"W"; // something 'wide' float fOneSpace = [dummyPaddingLetter calculateAccumulatedFrame].size.width; 

Now add as many multiples of fOneSpace as possible to the width of your rectangle, and this will probably be correct.

+1
source share

All Articles