文本渲染图:
CCLabelAtlas、CCLabelBMFont、CCLabelTTF类都是继承 CCLabelProtocol类,即可以使用系统字,也可以自定义渲染字体。
CCLabelAtlas类使用图片作为文字的一种方式, 通过图片直接定义
CCLabelAtlas *label0 = CCLabelAtlas::create("ASDE test ", "tuffy_bold_italic-charmap.png", 46, 64, ' ');//参数顺序:要显示字符,图片路径, 字符宽度,字符高度, 起始字符 label0->setPosition(ccp(100, 200)); label0->setOpacity(200); this->addChild(label0); CCLabelAtlas *label2 = CCLabelAtlas::create("456789123", "tuffy_bold_italic-charmap.png", 46, 64, ' '); label2->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); label2->setOpacity(200); this->addChild(label2); //文本闪烁动画 CCActionInterval *ac = CCFadeOut::create(1.2f); CCActionInterval *ac2 = ac->reverse(); label2->runAction(CCRepeatForever::create(CCSequence::create(ac, ac2, NULL))); //使用plist配置文件的描述来定义,可以根据需要修改配置文件信息,包括图片路径、字符宽度高度、起始字符 CCLabelAtlas* label1 = CCLabelAtlas::create("WER VBN", "./tuffy_bold_italic-charmap.plist");//参数顺序:要显示字符,plist文件路径 label1->setPosition( ccp(10,100) ); label1->setOpacity( 200 ); this->addChild(label1);
CCLabelTTF 类是通过系统字实现字体标签
CCLabelTTF *ttf = CCLabelTTF::create("HELLO WORLD ", "Helvetica", 30, ccp(320, 30), kCCTextAlignmentLeft);//参数顺序:要显示字符,字库名称,字号,范围大小,对齐方式[kCCTextAlignmentLeft(左对齐) kCCTextAlignmentRight(右对齐) kCCTextAlignmentCenter(中心对齐)] ttf->setPosition(ccp(300, 400)); this->addChild(ttf);
CCTextFieldTTF类输入框使用文字标签,继承CCLabelTTF类
CCTextFieldTTF *pTest = CCTextFieldTTF::textFieldWithPlaceHolder("<click hee for input>", "STHeitiTC-Light",40 ); pTest->setPosition(ccp(300, 500)); this->addChild(pTest);
CCLabelBMFont类中每个字都是一个精灵,每个字都可以定义动作,并支持FNT类型文件
CCLabelBMFont *label = CCLabelBMFont::create("Bitmap Font Atlas Xub", "fonts/bitmapFontTest.fnt"); addChild(label); CCSize s = CCDirector::sharedDirector()->getWinSize(); label->setPosition( ccp(s.width/2-200, s.height/2) ); label->setAnchorPoint( ccp(0.5f, 0.5f) ); CCSprite* BChar = (CCSprite*) label->getChildByTag(0);//获取第1字符'B' CCSprite* FChar = (CCSprite*) label->getChildByTag(7);//获取第7字符'F' CCSprite* AChar = (CCSprite*) label->getChildByTag(12);//获取第12字符'A' CCSprite* XChar = (CCSprite*) label->getChildByTag(18);//获取第12字符'A' //为字符创建动作 CCActionInterval* rotate = CCRotateBy::create(2, 360); CCAction* rot_4ever = CCRepeatForever::create(rotate); CCActionInterval* scale = CCScaleBy::create(2, 1.5f); CCActionInterval* scale_back = scale->reverse(); CCSequence* scale_seq = CCSequence::create(scale, scale_back,NULL); CCAction* scale_4ever = CCRepeatForever::create(scale_seq); CCActionInterval* jump = CCJumpBy::create(0.5f, CCPointZero, 60, 1); CCAction* jump_4ever = CCRepeatForever::create(jump); CCActionInterval* fade_out = CCFadeOut::create(1); CCActionInterval* fade_in = CCFadeIn::create(1); CCSequence* seq = CCSequence::create(fade_out, fade_in, NULL); CCAction* fade_4ever = CCRepeatForever::create(seq); CCActionInterval *by = CCMoveBy::create(1.0f, ccp(300, 300)); CCActionInterval *by2 = by->reverse(); CCSequence* seq_by = CCSequence::create(by, by2, NULL); CCAction *ac_by = CCRepeatForever::create(seq_by); BChar->runAction(rot_4ever); BChar->runAction(scale_4ever); FChar->runAction(jump_4ever); AChar->runAction(fade_4ever); XChar->runAction(ac_by);