There are three ways to display text in a Cocos2d-X:
Cclabelttf: The system font is used. Each character string generates a texture, which is less efficient and suitable for non-changing text.
Cclabelatlas: Uses nodeatlas to optimize rendering. It is suitable for numbers that change frequently, such as scores and money.
Cclabelbmfont: it is flexible to use ccspritebatchnode. Each character is an genie. You can operate on each character and use the image directly to draw the text.
Set the text font to cclabelttf.
// Set the attribute bool label: Init () {// initialize the parent class cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddirector ()-> getwinsize (); // create a font with the following words: cclabelttf * label = cclabelttf: Create ("this is text", "Arial ", 25); addchild (Label); // set the text position setposition (CCP (winsize. width/2, winsize. height/2); Return true ;}
Code parsing:
Cclabelttf * label = cclabelttf: Create ("this is text", "Arial", 25 );
First parameter: displayed text
Second parameter: text font
Third parameter: Text Size
Execution result:
Traverse all the cclabelttf Fonts
Add the following code to label. h:
#ifndef __Label_H__#define __Label_H__#include "cocos2d.h"USING_NS_CC ;class Label : public CCLayer{public: bool init(); static CCScene* scene(); bool ccTouchBegan(CCTouch*,CCEvent); void ccTouchMoved(CCTouch*, CCEvent*); CREATE_FUNC(Label);};#endif
Add the following code to label. cpp:
<Span style = "color: #000000;"> # include "label. H "using_ns_cc; // contains all the cclabeltitf fonts static const char * _ font [] = {" applegothic "," HiraKakuProN-W6 "," HiraKakuProN-W3 "," markerfelt-thin ", "stheitik-medium", "stheitik-light", "timesnewromanpsmt", "Helvetica-oblique", "Helvetica-boldoblique", "Helvetica", "Helvetica-bold ", "timesnewromanps-boldmt", "timesnewromanps-bolditalicmt", "timesnewromanps-italicmt", "verdana-bold", "verdana-bolditalic", "verdana", "verdana-italic ", "Georgia-bold", "Georgia", "Georgia-bolditalic", "Georgia-italic", "arialroundedmtbold", "trebuchetms-italic", "trebuchetms ", "trebuchet-bolditalic", "trebuchetms-bold", "stheititc-light", "stheititc-medium", "geezapro-bold", "geezapro", "Courier ", "courier-boldoblique", "courier-oblique", "courier-bold", "arialmt", "Arial-boldmt", "Arial-bolditalicmt", "Arial-italicmt ", "stheitij-medium", "stheitij-light", "arialhebrew", "arialhebrew-bold", "couriernewps-boldmt", "couriernewps-bolditalicmt", "success ", "thonburi-bold", "americantypewriter", "americantypewriter-bold", "stheitisc-medium", "stheitisc-light", "helveticaneue", "helveticaneue ", "helveticaneue-bold", "zapfino"}; ccscene * label: Scene () {// create a ccscene * s = ccscene: Create (); // create a cclayer * layer = label: Create (); // Add the scenario to the callback S-> addchild (layer); // return the scenario return s ;} // set the attribute bool label: Init () {// initialize the parent class cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddire()-> getwinsize (); int Height = 0; // traverse all fonts in the cclabeltif for (INT I = 0; I <sizeof (_ font) /sizeof (* _ font); I ++) {// create a font, and use this as the font. The text cclabelttf * label = cclabelttf: Create (_ font [I], _ font [I], 25); // obtain the font width and height. ccsize size = label-> boundingbox (). size; addchild (Label); // set the text position label-> setposition (CCP (winsize. width/2, height + size. height/2); height + = size. height;} settouchenabled (true); settouchmode (kcctouchesonebyone); Return true;} bool label: cctouchbegan (cctouch * touch, ccevent) {return true;} void label :: cctouchmoved (cctouch * touch, ccevent *) {// get the ccpoint ptdelta = touch-> getdelta (); // setpositiony (getpositiony () + ptdelta. y) ;}</span>
Execution result:
Use cclabelatlas to display color Fonts
First, you need a PNG Image and put it in the Resource Directory.
Then add the following code
// Set the attribute bool labelatlas: Init () {// initialize the parent cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddirector ()-> getwinsize (); // create an atlas font // The first parameter: displayed content // The second parameter: image name // The third parameter: the width of the image // The fourth parameter: The Image Height // The Fifth parameter: The ascall code value of the first word in the image, cclabelatlas * label = cclabelatlas: Create ("18937302 ", "labelatlasimg.png", 24, 32, '0'); addchild (Label); // you can specify the position label> setposition (winsize) for text display. width/2, winsize. height/2); Return true ;}
Execution result:
Add the following code to the code above to modify the text.
// Change the displayed number label-> setstring ("87899 ");
Execution result:
Use cclabelbmfont to display color words
First, you need a PNG Image and put it in the Resource Directory.
Then add the following code
// Set the attribute bool bmfont: Init () {// initialize the parent class cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddirector ()-> getwinsize (); // you can specify ccpoint ptcenter = CCP (winsize. width/2, winsize. height/2); // create an atlas font // The first parameter: displayed content // The second parameter: name of the description file of the image file: cclabelbmfont * label = cclabelbmfont :: create ("ABC", "bitmapfonttest. fnt "); addchild (Label); // set the text display position label-> setposition (ptcenter); Return true ;}
Execution result:
Use cclabelbmfont for rotating characters
// Set the attribute bool bmfont: Init () {// initialize the parent class cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddirector ()-> getwinsize (); // you can specify ccpoint ptcenter = CCP (winsize. width/2, winsize. height/2); // create an atlas font // The first parameter: displayed content // The second parameter: name of the description file of the image file: cclabelbmfont * label = cclabelbmfont :: create ("ABC", "bitmapfonttest. fnt "); addchild (Label); // set the text display position label-> setposition (ptcenter ); // enter the characters in the label field into the ccarray * arr = label-> getchildren (); // select the characters in the array, objectatindex (0) indicates selecting the 0th characters in the array ccsprite * sprite = (ccsprite *) Arr-> objectatindex (0); // The character rotates for 90 degrees sprite-> setrotation (90 ); return true ;}
Execution result:
Use cclabelbmfont to enlarge characters
// Set the attribute bool bmfont: Init () {// initialize the parent class cclayer: Init (); // set the window size ccsize winsize = ccdirector :: shareddirector ()-> getwinsize (); // you can specify ccpoint ptcenter = CCP (winsize. width/2, winsize. height/2); // create an atlas font // The first parameter: displayed content // The second parameter: name of the description file of the image file: cclabelbmfont * label = cclabelbmfont :: create ("ABC", "bitmapfonttest. fnt "); addchild (Label); // set the text display position label-> setposition (ptcenter ); // enter the characters in the label field into the ccarray * arr = label-> getchildren (); // select the characters in the array, objectatindex (0) indicates to select the 0th characters in the array ccsprite * sprite = (ccsprite *) Arr-> objectatindex (0); // enlarge the sprite-> setscale (2 ); return true ;}
Execution result:
Zookeeper
Fonts in Cocos2d-X