参考:http://www.2cto.com/kf/201404/295851.html
参考:http://blog.csdn.net/evankaka/article/details/43449943
#ifndef __ChineseString_H__
#define __ChineseString_H__
#include "cocos2d.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "..\cocos2d\external\win32-specific\icon\include\iconv.h"
#endif
#pragma comment(lib,"libiconv.lib")
#include "string"
using namespace std;
static char g_GBKConvUTF8Buf[5000] = { 0 };
class ChineseString
{
public:
static const char* GBKToUTF8(const char *strChar)//将字符串转成UFT-8
{
iconv_t iconvH = iconv_open("utf-8", "gb2312");
if (iconvH == 0)
{
return NULL;
}
size_t strLength = strlen(strChar);
size_t outLength = strLength * 4;
size_t copyLength = outLength;
memset(g_GBKConvUTF8Buf, 0, 5000);
char* outbuf = (char*)malloc(outLength);
char* pBuff = outbuf;
memset(outbuf, 0, outLength);
if (-1 == iconv(iconvH, &strChar, &strLength, &outbuf, &outLength))
{
iconv_close(iconvH);
return NULL;
}
memcpy(g_GBKConvUTF8Buf, pBuff, copyLength);
free(pBuff);
iconv_close(iconvH);
return g_GBKConvUTF8Buf;
}
};
#endif
如何调用:
Label *lb = Label::create(ChineseString::GBKToUTF8("字幕滚动应该是要在一个范围内可见的\n\n\n摇杆背景!!"), "", 30);
lb->setPosition(visibleSize / 2);
lb->setDimensions(400, 200);
this->addChild(lb);