如何在 cocos2d-x 中制作环绕的、可滚动的文本?

How to make wrapped, scrollable text in cocos2d-x?

我在 ui::Layout 中有一个 ui::Text,根据内容,它会溢出。我研究了 Label::setOverflowLabel::setWrap 关闭 ui::TextvirtualRenderer Label,但我没有看到使其可滚动和换行的方法.

如何制作可滚动的 ui::Text 并确保其正确换行?

诀窍是不使用 ui::Scrollview,而是使用 ui::ListView,里面只有一个项目,即 ui::Text。这使您可以在更改 ui::Text.

的文本内容时滚动并动态调整容器大小

关键是 a) 将 ui::Text 的宽度设置为与其父级 ui::ListView 相同的大小,并将高度设置为 0 和 b) 随时在列表视图上调用 my_listview->requestDoLayout()文本内容发生变化,因此可滚动区域反映了这一点。

下面是一个示例,说明如何将大文本滚动到较小的文本中ui::Panel

ui::ListView* listview = ListView::create();
my_scene->addChild(listview);
listview->setContentSize({300, 500}); //give it whatever size

ui::Text* text = ui::Text::create("[multiline content]", "fontName.ttf", 16);
text->setTextAreaSize({300, 0}); //use that same size, but use 0 height. This auto sets overflow and wrap in the backend
listview->addChild(text);
listview->requestDoLayout(); //this triggers resizing the listview to accommodate the
                             //string content. Needs to happen each time you
                             //text->setString(new_content) too.