文本环绕在Cocos2d-x中

Ohl*_*rol 5 c++ word-wrap cocos2d-iphone cocos2d-x

如果文本比它们所在的框长,我试图让我的文本标签自动调整大小.我还希望它支持多行功能.我在网上做了一些搜索,看到它曾经像这样工作:

CCLabelTTF::labelWithString(“This is a sentence longer than a line width.2d-x”, CGSizeMake(**0, 0**), UITextAlignmentCenter, “Thonburi”, 20);
Run Code Online (Sandbox Code Playgroud)

但似乎不再可以在cocos中使用,所以我不知道该怎么做..现在我的标签设置如下:

myQuestion = Label::createWithTTF("Testing to see if text wrap will work" ,c_strFontNameBase, 50);
myQuestion->setPosition(boxLabel->getContentSize().width/2, boxLabel->getContentSize().height/2);
boxLabel->addChild(myQuestion, 50);
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以使用类似于顶部示例的方式来使我的工作?这似乎不应该是非常困难的事情,但我发现它在线缺乏文档...

小智 6

我相信,您只能使标签的一个尺寸自动调整大小,即宽度或高度都可以自动调整大小.默认情况下,如下所示创建标签时,标签的宽度设置为使用固定高度自动调整大小:

auto label = Label::createWithTTF("Hello World gsdhsgdh gshdghsg yutywe gdgshdgy bnbjh hshhashgy hjnbdnsdhh ghhsgdhg ghghghsd ghhghsd ghghghgsd jkjkhsdjkj ououisdusydsi kkjkxncmxcjh kcxhjxhcjx jkuiushjxchxjch hjhjchxuyuychjc ", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);
Run Code Online (Sandbox Code Playgroud)

但是如果你想要多线支持,即固定宽度和可调整高度,你只需要设置固定宽度和零高度的标签尺寸:

label->setDimensions(300, 0);
Run Code Online (Sandbox Code Playgroud)

我希望它会有所帮助.