From db915e623d229c68078ee0d5f25d0780d571854c Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Wed, 8 May 2024 15:28:27 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0label=E9=80=82=E9=85=8D?= =?utf8?q?=E5=A4=9A=E5=9B=BD=E8=AF=AD=E8=A8=80=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/uicomponents/UIComponents.cpp | 63 ++++++++++++++----- .../nativeui/uicomponents/UIComponents.h | 7 ++- 2 files changed, 51 insertions(+), 19 deletions(-) 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 -- 2.17.1