cocos2dx控制元件-----ListView的使用(label自動換行)
阿新 • • 發佈:2019-01-05
float totalHeight = 0;
if (m_textInfoListView)
{
m_textInfoListView->removeAllChildrenWithCleanup(true);
}
m_textInfoListView = nullptr;
m_textInfoListView = ListView::create();
m_textInfoListView->setAnchorPoint(Vec2(0.5f,1.0f));//設定listview錨點
auto winSize = Director::getInstance()->getWinSize();
m_textInfoListView->setPosition(Vec2(winSize.width / 2, winSize.height*0.8));//設定ListView的位置
m_textInfoListView->setContentSize(Size(600, 950));//設定listview 的大小
m_bgPannel->addChild(m_textInfoListView);
//MKJsonDoc doc;解析json
if (doc.parseFile("configs/how_to_play_info.json"))
{
auto tipsArray_value = doc.getValue(StringUtils::format("tips_%s", key.c_str()).c_str());
if (tipsArray_value.isArray())
{
for (int i = 0; i < tipsArray_value.size(); ++i)
{
//create label begin
Label* label = Label::create();
label->setAnchorPoint(Vec2(0, 0.5f));
if (i == 0)
{
label->setSystemFontSize(48);
}
else
{
label->setSystemFontSize(36);
}
if ("score" == key)
{
if (i == 7 || i == 10)
{
label->setSystemFontSize(48);
}
}
//label->setLineBreakWithoutSpace(true);
//label->setMaxLineWidth(100);//m_textInfoListView->getContentSize().width - 60
label->setDimensions(m_textInfoListView->getContentSize().width,0);//label自動換行
label->setString(tipsArray_value.getValue(i).getString());
//label end
totalHeight += label->getContentSize().height;
//layout begin
Layout* layout = Layout::create();//把一個layout新增到listview中,layout裡面可以新增很多控制元件實現不同的效果。
layout->setAnchorPoint(Vec2(0, 0));
layout->setContentSize(Size(m_textInfoListView->getContentSize().width, label->getContentSize().height));//設定layout的高度
label->setPosition(Vec2(0, layout->getContentSize().height / 2));
layout->addChild(label, 10);
//layout->setBackGroundImageScale9Enabled(true);
//layout->setBackGroundImage("textures/settings/howToPlay/play_bg.png");
layout->setPosition(Vec2(30, 0));
//layout end
m_textInfoListView->pushBackCustomItem(layout);//往listview中新增資料
}
}
}
if (m_textInfoListView)
{
m_textInfoListView->removeAllChildrenWithCleanup(true);
}
m_textInfoListView = nullptr;
m_textInfoListView = ListView::create();
m_textInfoListView->setAnchorPoint(Vec2(0.5f,1.0f));//設定listview錨點
auto winSize = Director::getInstance()->getWinSize();
m_textInfoListView->setPosition(Vec2(winSize.width / 2, winSize.height*0.8));//設定ListView的位置
m_textInfoListView->setContentSize(Size(600, 950));//設定listview 的大小
m_bgPannel->addChild(m_textInfoListView);
//MKJsonDoc doc;解析json
if (doc.parseFile("configs/how_to_play_info.json"))
{
auto tipsArray_value = doc.getValue(StringUtils::format("tips_%s", key.c_str()).c_str());
if (tipsArray_value.isArray())
{
for (int i = 0; i < tipsArray_value.size(); ++i)
{
//create label begin
Label* label = Label::create();
label->setAnchorPoint(Vec2(0, 0.5f));
if (i == 0)
{
label->setSystemFontSize(48);
}
else
{
label->setSystemFontSize(36);
}
if ("score" == key)
{
if (i == 7 || i == 10)
{
label->setSystemFontSize(48);
}
}
//label->setLineBreakWithoutSpace(true);
//label->setMaxLineWidth(100);//m_textInfoListView->getContentSize().width - 60
label->setDimensions(m_textInfoListView->getContentSize().width,0);//label自動換行
label->setString(tipsArray_value.getValue(i).getString());
//label end
totalHeight += label->getContentSize().height;
//layout begin
Layout* layout = Layout::create();//把一個layout新增到listview中,layout裡面可以新增很多控制元件實現不同的效果。
layout->setAnchorPoint(Vec2(0, 0));
layout->setContentSize(Size(m_textInfoListView->getContentSize().width, label->getContentSize().height));//設定layout的高度
label->setPosition(Vec2(0, layout->getContentSize().height / 2));
layout->addChild(label, 10);
//layout->setBackGroundImageScale9Enabled(true);
//layout->setBackGroundImage("textures/settings/howToPlay/play_bg.png");
layout->setPosition(Vec2(30, 0));
//layout end
m_textInfoListView->pushBackCustomItem(layout);//往listview中新增資料
}
}
}