我有一个ui::Text
,ui::Layout
根据内容,它会溢出。我已经查看Label::setOverflow
和Label::setWrap
关闭了ui::Text
's virtualRenderer
Label
,但我没有看到使其可滚动和换行的方法。
如何ui::Text
在确保正确包装文本的同时制作可滚动的?
我有一个ui::Text
,ui::Layout
根据内容,它会溢出。我已经查看Label::setOverflow
和Label::setWrap
关闭了ui::Text
's virtualRenderer
Label
,但我没有看到使其可滚动和换行的方法。
如何ui::Text
在确保正确包装文本的同时制作可滚动的?
诀窍是,而不是使用ui::Scrollview
,使用 aui::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.