从外部文件读取UTF-8
推荐大家使用资源文件进行配置保存,如xml将其采用的UTF-8的编码方式保存,自然会让我想到,日文、韩文等待各种国家的语言都可以正常显示了,为了你的软件国际化...尽量采用这种方式吧!到时候根据手机系统的语言,然后动态的来读取你文件中的资源...
先看下我们的xml文件:
- <dict>
- <key>chinese1</key>
- <string>美好的一天</string>
- <key>japanese</key>
- <string>良い一日を</string>
- <key>spanish</key>
- <string>Buen día</string>
- </dict>
- </plist>
然后再来看如何使用:
- //利用CCDictionary来读取xml
- CCDictionary *strings = CCDictionary::create("fonts/strings.xml");
- //中文,日语,西班牙语:objectForKey根据key,获取对应的string
- const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->m_sString.c_str();
- const char *japanese = ((CCString*)strings->objectForKey("japanese"))->m_sString.c_str();
- const char *spanish = ((CCString*)strings->objectForKey("spanish"))->m_sString.c_str();
- CCLabelBMFont *label1 = CCLabelBMFont::create(spanish, "fonts/arial-unicode-26.fnt");
- addChild(label1);
- label1->setPosition(ccp(s.width/2, s.height/4*3-20));
- CCLabelBMFont *label2 = CCLabelBMFont::create(chinese, "fonts/arial-unicode-26.fnt");
- addChild(label2);
- label2->setPosition(ccp(s.width/2, s.height/4*2));
- CCLabelBMFont *label3 = CCLabelBMFont::create(japanese, "fonts/arial-unicode-26.fnt");
- addChild(label3);
- label3->setPosition(ccp(s.width/2, s.height/4*1));
运行效果: