From: Caisking <3449750381@qq.com> Date: Wed, 8 May 2024 07:28:27 +0000 (+0800) Subject: 增加label适配多国语言函数 X-Git-Tag: release_V1.2.70_20241020_135402~1025^2~20 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=db915e623d229c68078ee0d5f25d0780d571854c;p=mirrors%2FFD230.git 增加label适配多国语言函数 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp index 87c9fd1ed..284cf60c3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp @@ -268,37 +268,66 @@ namespace OHOS InitLabel(label, x, y, width, height, viewId, text, fontName, fontSize); } - /*更新label的数字*/ - void UpdateLabel(UILabelExt *&label, int num) + /*替换字符串中的变量*/ + 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 diff --git a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h index 7b0164af3..88027cfb5 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h +++ b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h @@ -52,10 +52,13 @@ namespace OHOS /*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