码迷,mamicode.com
首页 > 其他好文 > 详细

(2)Cocos2d-x中文显示

时间:2015-03-10 16:41:41      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

  Cocos使用UTF8编码来处理文字, 要显示中文需要将中文转换为UTF8编码,这里使用xml来保存文字资源。

StringResources_zh.xml

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>hello_world</key>
    <string>你好,世界!</string>
</dict>
</plist>

StringResourcesManager.h

 1 #ifndef StringResourcesManager_h__
 2 #define StringResourcesManager_h__
 3 #include "cocos2d.h"
 4 USING_NS_CC;
 5 
 6 class StringResourcesManager
 7 {
 8 public:
 9     static StringResourcesManager* GetInstance()
10     {
11         if (Instance == NULL)
12         {
13             Instance = new StringResourcesManager();
14         }
15         return Instance;
16     }
17 
18     std::string GetString(const std::string& key)
19     {
20         return valueMap[key].asString();
21     }
22     void DestroyInstance()
23     {
24         CC_SAFE_DELETE(Instance);
25     }
26     ~StringResourcesManager()
27     {
28     }
29 private:
30     static StringResourcesManager* Instance;
31     ValueMap valueMap;
32     CC_DISALLOW_COPY_AND_ASSIGN(StringResourcesManager);
33     StringResourcesManager()
34     {
35         if (Application::getInstance()->getCurrentLanguage()
36             == LanguageType::CHINESE) {
37             valueMap = FileUtils::getInstance()->getValueMapFromFile(
38                 "text/StringResources_zh.xml");
39         }
40         else {
41             valueMap = FileUtils::getInstance()->getValueMapFromFile(
42                 "text/StringResources_en.xml");
43         }
44     }
45 
46 };
47 
48 StringResourcesManager* StringResourcesManager::Instance = nullptr;
49 
50 #endif // StringResourcesManager_h__

使用

1 TTFConfig ttfConfig("fonts/simhei.ttf", 35, GlyphCollection::ASCII);
2 auto label = Label::createWithTTF(ttfConfig, StringResourcesManager::GetInstance()->GetString("hello_world"), TextHAlignment::CENTER, visibleSize.width);
3 label->setAnchorPoint(Vec2(0,1));
4 label->setPosition(0,visibleSize.height);
5 this->addChild(label);

 

(2)Cocos2d-x中文显示

标签:

原文地址:http://www.cnblogs.com/xyzaabb/p/4326142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!