Cocos2d-x中添加标签一共有三种方法:CCLabelTTF、CCLabelAtlas、CCLabelBMFont,我们逐一学习。
一、CCLabelTTF
CCLabelTTF类中部分代码如下,其中都是常用的函数:
class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
{
public:
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize);
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment);
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment);
static CCLabelTTF * createWithFontDefinition(const char *string, ccFontDefinition &textDefinition);
void setTextDefinition(ccFontDefinition *theDefinition);
ccFontDefinition * getTextDefinition();
static CCLabelTTF * create();
virtual void setString(const char *label);
virtual const char* getString(void);
CCTextAlignment getHorizontalAlignment();
void setHorizontalAlignment(CCTextAlignment alignment);
CCVerticalTextAlignment getVerticalAlignment();
void setVerticalAlignment(CCVerticalTextAlignment verticalAlignment);
CCSize getDimensions();
void setDimensions(const CCSize &dim);
float getFontSize();
void setFontSize(float fontSize);
const char* getFontName();
void setFontName(const char *fontName);
}
由上述代码可以看到有两种创建CCLabelTTF的方法,第一种通过create创建,第二种通过createWithFontDefinition创建。
(一)通过create创建CCLabelTTF:
一共有四个create重载函数如下:
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize);
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment);
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment);
static CCLabelTTF * create();
由上面代码可以看出三个create函数部分参数一致,再来看看create具体实现:
首先看看参数为空的create():
CCLabelTTF * CCLabelTTF::create()
{
CCLabelTTF * pRet = new CCLabelTTF();
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
参数为空的create()仅仅是创建了一个CCLabelTTF对象,没有具体内容。
再来看看另外三个有参数的create()函数:
CCLabelTTF * CCLabelTTF::create(const char *string, const char *fontName, float fontSize)
{
return CCLabelTTF::create(string, fontName, fontSize,
CCSizeZero, kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop);
}
CCLabelTTF * CCLabelTTF::create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment)
{
return CCLabelTTF::create(string, fontName, fontSize, dimensions, hAlignment, kCCVerticalTextAlignmentTop);
}
CCLabelTTF* CCLabelTTF::create(const char *string, const char *fontName, float fontSize,
const CCSize &dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment)
{
CCLabelTTF *pRet = new CCLabelTTF();
if(pRet && pRet->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
由此可以知道三个参数的create()与五个参数的create()都调用的是有六个参数的create(),缺少的参数设置为默认值,具体讲解一下参数内容:
第一个参数const char *string:需要显示的字符串。
第二个参数const char *fontName:字体。
第三个参数float fontSize:字号。
第四个参数const CCSize &dimensions:要显示的宽和高。
第五个参数 CCTextAlignment hAlignment:水平方向对齐。
第六个参数CCVerticalTextAlignment vAlignment:垂直方向对齐。
关于水平与垂直对齐方式,有如下定义:
typedef enum
{
kCCVerticalTextAlignmentTop,
kCCVerticalTextAlignmentCenter,
kCCVerticalTextAlignmentBottom,
} CCVerticalTextAlignment;
// XXX: If any of these enums are edited and/or reordered, update CCTexture2D.m
//! Horizontal text alignment type
typedef enum
{
kCCTextAlignmentLeft,
kCCTextAlignmentCenter,
kCCTextAlignmentRight,
} CCTextAlignment;
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* ttf3 = CCLabelTTF::create("JianYiLiGong3", "Courier", 50);
ttf3->setPosition(ccp(winSize.width / 2, winSize.height / 2) + ccp(0,100));
addChild(ttf3);
CCLabelTTF* ttf5 = CCLabelTTF::create("JianYiLiGong", "Courier", 50, CCSizeMake(50, 50), kCCTextAlignmentLeft);
ttf5->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(ttf5);
CCLabelTTF* ttf6 = CCLabelTTF::create("JianYiLiGong", "Courier", 50, CCSizeMake(100, 100), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
ttf6->setPosition(ccp(winSize.width / 2, winSize.height / 2) - ccp(0, 100));
addChild(ttf6);
(二)通过createWithFontDefinition创建CCLabelTTF
static CCLabelTTF * createWithFontDefinition(const char *string, ccFontDefinition &textDefinition);
由上述代码可以看出createWithFontDefinition的第一个参数为要显示的字符串,第二个参数为一个ccFontDefinition类型的参数。
ccFontDefinition中定义显示字符串的一些信息,具体代码如下:
typedef struct _ccFontDefinition
{
public:
_ccFontDefinition() : m_alignment(kCCTextAlignmentCenter),
m_vertAlignment(kCCVerticalTextAlignmentTop),
m_fontFillColor(ccWHITE)
{
m_dimensions = CCSizeMake(0, 0);
}
// font name
std::string m_fontName;
// font size
int m_fontSize;
// horizontal alignment
CCTextAlignment m_alignment;
// vertical alignment
CCVerticalTextAlignment m_vertAlignment;
// renering box
CCSize m_dimensions;
// font color
ccColor3B m_fontFillColor;
// font shadow
ccFontShadow m_shadow;
// font stroke
ccFontStroke m_stroke;
} ccFontDefinition;
结构体ccFontDefinition具体解释如下:
std::string m_fontName:字体。int m_fontSize:字号。
CCTextAlignment m_alignment:水平对齐方式。
CCVerticalTextAlignment m_vertAlignment:垂直对齐方式。
CCSize m_dimensions:显示的大小。
ccColor3B m_fontFillColor:字体的颜色。
ccFontShadow m_shadow:是否有阴影。
ccFontStroke m_stroke:边框。
ccFontShadow的定义如下:
typedef struct _ccFontShadow
{
public:
// shadow is not enabled by default
_ccFontShadow(): m_shadowEnabled(false) {}
// true if shadow enabled
bool m_shadowEnabled;
// shadow x and y offset
CCSize m_shadowOffset;
// shadow blurrines
float m_shadowBlur;
// shadow opacity
float m_shadowOpacity;
} ccFontShadow;
ccFontStroke的定义如下:
typedef struct _ccFontStroke
{
public:
// stroke is disabled by default
_ccFontStroke(): m_strokeEnabled(false) {}
// true if stroke enabled
bool m_strokeEnabled;
// stroke color
ccColor3B m_strokeColor;
// stroke size
float m_strokeSize;
} ccFontStroke;
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
ccFontDefinition fontDef;
fontDef.m_fontName = "Courier";
fontDef.m_fontSize = 10;
fontDef.m_alignment = kCCTextAlignmentCenter;
fontDef.m_vertAlignment = kCCVerticalTextAlignmentBottom;
fontDef.m_dimensions = CCSizeMake(50, 100);
fontDef.m_fontFillColor = ccGREEN;
fontDef.m_shadow.m_shadowEnabled = false;
fontDef.m_shadow.m_shadowOffset = CCSizeMake(5,5);
fontDef.m_shadow.m_shadowOpacity = 1.0f;
fontDef.m_shadow.m_shadowBlur = 0.5f;
fontDef.m_stroke.m_strokeEnabled = true;
fontDef.m_stroke.m_strokeColor = ccBLACK;
fontDef.m_stroke.m_strokeSize = 1.f;
CCLabelTTF* ttf = CCLabelTTF::createWithFontDefinition("JianYiLiGong", fontDef);
ttf->setPosition(ccp(winSize.width/2, winSize.height/2));
addChild(ttf);
(三)修改CCLabelTTF内容
void setTextDefinition(ccFontDefinition *theDefinition);
ccFontDefinition * getTextDefinition();
virtual void setString(const char *label);
virtual const char* getString(void);
CCTextAlignment getHorizontalAlignment();
void setHorizontalAlignment(CCTextAlignment alignment);
CCVerticalTextAlignment getVerticalAlignment();
void setVerticalAlignment(CCVerticalTextAlignment verticalAlignment);
CCSize getDimensions();
void setDimensions(const CCSize &dim);
float getFontSize();
void setFontSize(float fontSize);
const char* getFontName();
void setFontName(const char *fontName);
已经设置好的字体、字号、显示的字符串等可以通过以上方法进行修改。
优点:简单易操作,无需任何额外的资源
缺点:由于它的运行原理,是先将字符转化为图片纹理,然后渲染至屏幕。所以不适就于变动的文字。易于静态显示。
CCLabelAtlas类中部分代码如下:
class CC_DLL CCLabelAtlas : public CCAtlasNode, public CCLabelProtocol
{
public:
static CCLabelAtlas * create(const char *string, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap);
static CCLabelAtlas* create(const char *string, const char *fntFile);
virtual void setString(const char *label);
virtual const char* getString(void);
}
由上面代码可以看出create有两个,一个create有两个参数,一个create有四个参数。
(一)有四个参数的create:
static CCLabelAtlas * create(const char *string, const char *charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap);
const char *string:我们要显示的字符串。
const char *charMapFile:字符集图,包含我们要显示的字符串的图片文件,.png格式。
unsigned int itemWidth:图片文件中每个字符的宽。
unsigned int itemHeight:图片文件中每个字符的高。
unsigned int startCharMap:图片文件中第一个字符的ASCII码。
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelAtlas* atlas = CCLabelAtlas::create("239","Labelatlas.png",24,32,'0');
atlas->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(atlas);
(二)有两个参数的create
static CCLabelAtlas* create(const char *string, const char *fntFile);
const char *string:我们要显示的字符串。
const char *fntFile:字体资源文件,.plist格式。
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelAtlas* atlas = CCLabelAtlas::create("JianYiLiGong", "font.plist");
atlas->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(atlas);
(三)对已显示的字符串操作
CCLabelAtlas类中提供了两个方法用来提取已经显示的字符串与设置显示字符串,如下:
virtual void setString(const char *label);
virtual const char* getString(void);
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelAtlas* atlas = CCLabelAtlas::create("239", "Labelatlas.png", 24, 32, '0');
atlas->setPosition(ccp(winSize.width / 2, winSize.height / 2));
CCLOG("%s",atlas->getString());
atlas->setString("932");
addChild(atlas);
缺点:素材需要依赖于美工,显示内容局限性大。
三、CCLabelBMFont
CCLabelBMFont类中部分代码如下:
class CC_DLL CCLabelBMFont : public CCSpriteBatchNode, public CCLabelProtocol, public CCRGBAProtocol
{
public:
static CCLabelBMFont * create(const char *str, const char *fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset);
static CCLabelBMFont * create(const char *str, const char *fntFile, float width, CCTextAlignment alignment);
static CCLabelBMFont * create(const char *str, const char *fntFile, float width);
static CCLabelBMFont * create(const char *str, const char *fntFile);
static CCLabelBMFont * create();
}
(一)创建CCLabelBMFont
上述5个create函数参数个数不同,参数类型部分相同,下面来看看它们具体实现,代码如下:CCLabelBMFont * CCLabelBMFont::create()
{
CCLabelBMFont * pRet = new CCLabelBMFont();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile, float width, CCTextAlignment alignment)
{
return CCLabelBMFont::create(str, fntFile, width, alignment, CCPointZero);
}
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile, float width)
{
return CCLabelBMFont::create(str, fntFile, width, kCCTextAlignmentLeft, CCPointZero);
}
CCLabelBMFont * CCLabelBMFont::create(const char *str, const char *fntFile)
{
return CCLabelBMFont::create(str, fntFile, kCCLabelAutomaticWidth, kCCTextAlignmentLeft, CCPointZero);
}
CCLabelBMFont *CCLabelBMFont::create(const char *str, const char *fntFile, float width/* = kCCLabelAutomaticWidth*/, CCTextAlignment alignment/* = kCCTextAlignmentLeft*/, CCPoint imageOffset/* = CCPointZero*/)
{
CCLabelBMFont *pRet = new CCLabelBMFont();
if(pRet && pRet->initWithString(str, fntFile, width, alignment, imageOffset))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
参数为空的create创建一个空的CCLabelBMFont,而其他的create最终都是调用参数最多的create。
下面解释一下create各个参数的意思:
const char *str:要显示的字符串。
const char *fntFile:字体位图文件,格式为.fnt。
float width:没搞明白什么意思,默认值是kCCLabelAutomaticWidth。
CCTextAlignment alignment:水平对齐方式,默认值kCCTextAlignmentLeft。
CCPoint imageOffset:图片中字符的位移,默认值CCPointZero。
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelBMFont* bmFont = CCLabelBMFont::create("good", "bitmapFontTest3.fnt");
bmFont->setPosition(ccp(winSize.width/2,winSize.height/2));
addChild(bmFont);
(二)使用CCLabelBMFont
CCLabelBMFont 继承自CCSpriteBatchNode,所以本身采用了CCSpriteBatchNode 的优化功能。CCLabelBMFont 中的每一个字符都是一个己加载到CCSpriteBatchNode中的CCSprite。可以通过接口取出。这种实现方式既实现了优化的效果,也更灵活。
实例:
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelBMFont* bmFont = CCLabelBMFont::create("good", "bitmapFontTest3.fnt");
bmFont->setPosition(ccp(winSize.width/2,winSize.height/2));
CCArray* arr = bmFont->getChildren();
CCSprite* first = (CCSprite*)arr->objectAtIndex(0);
CCJumpBy* jump = CCJumpBy::create(2, ccp(0, 0), 10, 3);
first->runAction(jump);
addChild(bmFont);
创建的CCLabelBMFont对象,其中每个字符可以通过getChildren()返回到一个CCArray中,通过CCArray来操作 CCLabelBMFont中每个字符。
CCLabelBMFont还有许多其他函数可以用,暂时不继续探究了。
优点 : 显示字体多样,内部完成优化效率高。
缺点 : 需要依赖美工制作fnt 文件。