SKLabelNode removes leading and trailing spaces - How can I stop that?

喜你入骨 提交于 2019-12-08 22:08:18

问题


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


回答1:


If you simply want labels to line up so that they are right aligned, then use a right alignment mode.

myLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;

However, if you are trying to create a rectangle around the SKLabelNode, then you'll find calculateAccumulatedFrame does indeed strip any spaces before calculating the width of the label, and returns the wrong size.

But you can cheat - use a character as a dummy sizing metric to add onto your frame size.

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 to the width of your rectangle, and it'll probably be about right.



来源:https://stackoverflow.com/questions/22632433/sklabelnode-removes-leading-and-trailing-spaces-how-can-i-stop-that

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!