<版本:cocos2d-x——V2.2.1>
主角:CCLabelTTF
与换行相关的静态成员函数create:
CCLabelTTF * CCLabelTTF::create(
const char *string, // 显示字符串
const char *fontName, // 字体名
float fontSize, // 字体大小
const CCSize& dimensions, // 显示尺寸
CCTextAlignment hAlignment); // 水平对齐方式
// kCCTextAlignmentLeft左对齐
// kCCTextAlignmentCenter居中
// kCCTextAlignmentRight右对齐
CCLabelTTF* CCLabelTTF::create(
const char *string,
const char *fontName,
float fontSize,
const CCSize &dimensions,
CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment); // 垂直对齐方式
// kCCVerticalTextAlignmentTop顶对齐
// kCCVerticalTextAlignmentCenter居中
// kCCVerticalTextAlignmentBottom底对齐
多行显示方式:
1.使用以上提到之外的其他create方法:在字符串参数中,插入'\n'字符
2.使用上述create方法:dimensions参数表示标签显示范围
对于第一种,我们需要控制到底应在何处插入'\n'字符,比较麻烦;选择第二种比较傻瓜式一点,方便使用
贴上示例代码(为了避免编码的问题,字符串使用纯英文):
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCSize size = CCDirector::sharedDirector()->getWinSize();
string str = "Silent night, holy night,All is calm, all is bright."
"Round your Virgin, Mother and Child.Holy infant so tender and mild,Sleep in heavenly peace,"
"Sleep in heavenly peace.Silent night, holy night,"
"Shepherds quake at the sight.Glories stream from heaven afar,"
"Heavenly hosts sing Alleluia,Christ the Savior is born,"
"Christ the Savior is born.";
CCLabelTTF* label = CCLabelTTF::create(str.c_str(),"Arial",35,size,kCCTextAlignmentLeft);
label->setAnchorPoint(CCPointZero);
label->setPosition(CCPointZero);
this->addChild(label);
bRet = true;
} while (0);
return true;
}
效果图如下:
能力有限,希望对初学者有一丝丝的帮助!