增加label适配多国语言函数
authorCaisking <3449750381@qq.com>
Wed, 8 May 2024 07:28:27 +0000 (15:28 +0800)
committerCaisking <3449750381@qq.com>
Wed, 8 May 2024 07:28:27 +0000 (15:28 +0800)
code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp
code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h

index 87c9fd1..284cf60 100644 (file)
@@ -268,37 +268,66 @@ namespace OHOS
         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
index 7b0164a..88027cf 100644 (file)
@@ -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