SKLabelNode删除前导和尾随空格-如何停止呢? [英] SKLabelNode removes leading and trailing spaces - How can I stop that?

查看:120
本文介绍了SKLabelNode删除前导和尾随空格-如何停止呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个SKLabelNode,它应该始终具有相同的长度(对于猜单词游戏而言).不幸的是,SKLabelNode决定始终切掉任何前导和尾随空格.文档中未描述此行为.如何避免/关闭该行为?

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?

推荐答案

如果您只是希望将标签对齐以使它们右对齐,请使用右对齐模式.

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

myLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;

但是,如果您尝试在SKLabelNode周围创建一个矩形,那么您会发现,calculateAccumulatedFrame确实在计算标签宽度之前会去除任何空格,并返回错误的大小.

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;

现在将fOneSpace的倍数添加到矩形的宽度中,它大概是正确的.

Now add as many multiples of fOneSpace to the width of your rectangle, and it'll probably be about right.

这篇关于SKLabelNode删除前导和尾随空格-如何停止呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆