Cocos2d-x引擎当中为开发者提供了专门的用于绘制文字的类以及方法。引擎中用于处理文字的三个类分别是:CCLabelAtlas、CCLabelBMFont和CCLabelTTF,这三个类都是通过不同的方式来显示文字的。
1、CCLabelTTF:
/* CCLabelTTF类的创建函数 */
// 创建TTF标签对象,参数为显示文字内容、字体名称和字体尺寸
static CCLabelTTF *create(const char *string, const char *fontName, float fontSize);
// 创建TTF标签对象,参数为显示文字内容、字体名称、字体尺寸、标签尺寸、文字水平对齐方式
static CCLabelTTF *create(const char *string, const char *fontName, float fontSize,
const CCSize &dimensions, CCTextAlignment hAlignment);
// 创建TTF标签对象,参数为显示文字内容、字体名称、字体尺寸、标签尺寸、文字水平对齐方式、文字垂直对齐方式
static CCLabelTTF *create(const char *string, const char *fontName, float fontSize,
const CCSize &dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment);
void HelloWorld::showFont(const char *pFont)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSize blockSize = CCSizeMake(size.width/3, 200);
float fontSize = 26;
CCLabelTTF *left = CCLabelTTF::create("alignment left", pFont, fontSize, blockSize, kCCTextAlignmentLeft, kCCVerticalTextAlignmentTop);
CCLayerColor *leftColor = CCLayerColor::create(ccc4(100, 100, 100, 255), blockSize.width, blockSize.height);
leftColor->ignoreAnchorPointForPosition(false);
left->setAnchorPoint(ccp(0, 0.5));
leftColor->setAnchorPoint(ccp(0, 0.5));
left->setPosition(ccp(0, size.height/2));
leftColor->setPosition(left->getPosition());
this->addChild(leftColor,-1);
this->addChild(left, 0);
}