InitLabel(label, x, y, width, height, viewId, text, fontName, fontSize);
}
- /*æ\9b´æ\96°labelç\9a\84æ\95°å\97*/
- void UpdateLabel(UILabelExt *&label, int num)
+ /*æ\9b¿æ\8d¢å\97符串ä¸ç\9a\84å\8f\98é\87\8f*/
+ void UpdateLabelText(UILabelExt *&label, const std::string &newText)
{
+ if (label == nullptr)
+ {
+ return;
+ }
+
std::string str = label->GetText();
- std::string numStr = std::to_string(num);
- printf("UpdateLabel:%s->%d\r\n", str.c_str(), num);
+ printf("UpdateLabel:%s->%s\r\n", str.c_str(), newText.c_str());
// 找到标志"XX"在字符串中的位置
size_t pos = str.find("XX");
if (pos != std::string::npos)
{
- // 替换标志"XX"为数字字符串
- str.replace(pos, 2, numStr); // 2 是标志"XX"的长度
+ // 替换标志"XX"为更新字符串
+ str.replace(pos, 2, newText); // 2 是标志"XX"的长度
+ label->SetText(str.c_str());
+ }
+ }
+ /*更新数字*/
+ void UpdateLabel(UILabelExt *&label, int num)
+ {
+ if (label == nullptr)
+ {
+ return;
}
- label->SetText(str.c_str());
+ UpdateLabelText(label, std::to_string(num));
}
- /*更新label的字符串*/
- void UpdateLabel(UILabelExt *&label, const std::string updatestr)
+ /*更新文本*/
+ void UpdateLabel(UILabelExt *&label, const std::string &updateStr)
{
- std::string str = label->GetText();
- printf("UpdateLabel:%s->%s\r\n", str.c_str(), updatestr.c_str());
- // 找到标志"XX"在字符串中的位置
- size_t pos = str.find("XX");
- if (pos != std::string::npos)
+ if (label == nullptr)
+ {
+ return;
+ }
+
+ UpdateLabelText(label, updateStr);
+ }
+
+ /*设置label的Id*/
+ void SetLabelId(UILabel *&label, UIResourceTextId id)
+ {
+ if (label == nullptr)
+ {
+ return;
+ }
+
+ UILabelExt *labelext = new UILabelExt();
+ labelext->SetTextId(id);
+
+ const std::string text = labelext->GetText();
+ if (!text.empty())
{
- // 替换标志"XX"为数字字符串
- str.replace(pos, 2, updatestr); // 2 是标志"XX"的长度
+ printf("SetLabelId:%s\r\n", text.c_str());
+ label->SetText(text.c_str());
}
- label->SetText(str.c_str());
+ delete labelext;
}
}
\ No newline at end of file
/*InitLabel[默认参数]*/
void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, const char *viewId, const char *text, int fontSize = 30, const char *fontName = DEFAULT_VECTOR_FONT_FILENAME);
+ /*通用替换*/
+ void UpdateLabelText(UILabelExt *&label, const std::string &newText);
/*更新label的数字*/
void UpdateLabel(UILabelExt *&label, int num);
/*更新label的字符串*/
- void UpdateLabel(UILabelExt *&label, const std::string updatestr);
-
+ void UpdateLabel(UILabelExt *&label, const std::string &updatestr);
+ /*通过labelext获取Id设置label*/
+ void SetLabelId(UILabel *&label, UIResourceTextId id);
}
#endif // UICOMPONENTS_H
\ No newline at end of file