//将ui编辑器导出的json文件加入到项目中。
Layout *m_pBuild_root = static_cast<Layout*>(GUIReader::getInstance()->widgetFromJsonFile("Ui/BaseUi_8_1.json"));
this->addChild(m_pBuild_root);
//对ui编辑器中图片的处理
ImageView * buildImg = static_cast<ImageView*>(Helper::seekWidgetByName(m_pBuild_root, "build_img"));//get ImageView
buildImg->loadTexture("build.png"); //Load texture for imageview.
//对应文本框
Text* m_label = static_cast<Text*>(Helper::seekWidgetByName(m_pBuild_root,"Label_1"));
m_label->setText(Value("safds").getDescription().c_str()); //Changes the string value of label.
m_label->setColor(ccColor3B::RED);
int a=111;
int b=222;
int c=333;
//对应输入框
TextField * textfield= static_cast<TextField*>(Helper::seekWidgetByName(m_pBuild_root, "TextField_11"));
textfield->setText(Value(b).getDescription().c_str());
//对应数字标签
TextAtlas * atlas=static_cast<TextAtlas*>(Helper::seekWidgetByName(m_pBuild_root, "AtlasLabel_17"));
atlas->setStringValue(Value(c).getDescription().c_str());
//对应自定义字体
TextBMFont*bmf = static_cast<TextBMFont*>(Helper::seekWidgetByName(m_pBuild_root,"BitmapLabel_16"));
bmf->setText(Value(a).getDescription().c_str());
Tips:
1、层容器在ui编辑器中不提供锚点设置功能,默认为左下角,如需更改(如需要在项目中整体缩放)可在代码中更改锚点设置。
for (int i =1; i <7; i++)
{
Widget *panel=(Widget*)m_pBuild_root->getChildByTag(100);
Widget *panel2=(Widget*)panel->getChildByTag(i);
if (panel2!=NULL)
{
Rect rect=panel2->getBoundingBox();
Point pos=panel2->getPosition();
panel2->setAnchorPoint(Point(0.5,0.5));
panel2->setPosition(Point(pos.x+rect.size.width/2,pos.y+rect.size.height/2));
}
}
2、如果不想控件响应触摸事件,可在编辑器中关闭交互。