在Cocos2d—X游戏开发中,CCLabelTTF 和 CCSprite 大概是使用最多的2个类了。标签主要用于显示静态文本,可以设置字体的大小和位置等属性。
现在,我们先来看下CCLabelTTF 的基本源码。
S1,从下面的代码可以看到 CCLabelTTF 继承于 CCSprite 和 CCLabeProtocol 。
class CC_DLL CCLabelTTF : public CCSprite, public CCLabelProtocol
{
public:
CCLabelTTF();
virtual ~CCLabelTTF();
const char* description();
/** creates a CCLabelTTF with a font name and font size in points
@since v2.0.1 使用最多的创建标签的方法
*/
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize);
/** creates a CCLabelTTF from a fontname, horizontal alignment, dimension in points, and font size in points.
@since v2.0.1
*/
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment);
/** creates a CCLabel from a fontname, alignment, dimension in points and font size in points
@since v2.0.1
*/
static CCLabelTTF * create(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment);
/** Create a lable with string and a font definition*/
static CCLabelTTF * createWithFontDefinition(const char *string, ccFontDefinition &textDefinition);
/** initializes the CCLabelTTF with a font name and font size */
bool initWithString(const char *string, const char *fontName, float fontSize);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithString(const char *string, const char *fontName, float fontSize,
const CCSize& dimensions, CCTextAlignment hAlignment,
CCVerticalTextAlignment vAlignment);
/** initializes the CCLabelTTF with a font name, alignment, dimension and font size */
bool initWithStringAndTextDefinition(const char *string, ccFontDefinition &textDefinition);
/** set the text definition used by this label */
void setTextDefinition(ccFontDefinition *theDefinition);
/** get the text definition used by this label */
ccFontDefinition * getTextDefinition();
/** enable or disable shadow for the label */
void enableShadow(const CCSize &shadowOffset, float shadowOpacity, float shadowBlur, bool mustUpdateTexture = true);
/** disable shadow rendering */
void disableShadow(bool mustUpdateTexture = true);
/** enable or disable stroke */
void enableStroke(const ccColor3B &strokeColor, float strokeSize, bool mustUpdateTexture = true);
/** disable stroke */
void disableStroke(bool mustUpdateTexture = true);
/** set text tinting */
void setFontFillColor(const ccColor3B &tintColor, bool mustUpdateTexture = true);
/** initializes the CCLabelTTF */
bool init();
/** Creates an label.
*/
static CCLabelTTF * create();
/** changes the string to render
* @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas
*/
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);
private:
bool updateTexture();
protected:
/** set the text definition for this label */
void _updateWithTextDefinition(ccFontDefinition & textDefinition, bool mustUpdateTexture = true);
ccFontDefinition _prepareTextDefinition(bool adjustForResolution = false);
/** Dimensions of the label in Points */
CCSize m_tDimensions;
/** The alignment of the label */
CCTextAlignment m_hAlignment;
/** The vertical alignment of the label */
CCVerticalTextAlignment m_vAlignment;
/** Font name used in the label */
std::string * m_pFontName;
/** Font size of the label */
float m_fFontSize;
/** label's string */
std::string m_string;
/** font shadow */
bool m_shadowEnabled;
CCSize m_shadowOffset;
float m_shadowOpacity;
float m_shadowBlur;
/** font stroke */
bool m_strokeEnabled;
ccColor3B m_strokeColor;
float m_strokeSize;
/** font tint */
ccColor3B m_textFillColor;
};
好的,现在我们来看下CCTextAlignment的定义,是2个枚举类型。
typedef enum
{
kCCVerticalTextAlignmentTop,
kCCVerticalTextAlignmentCenter,
kCCVerticalTextAlignmentBottom,
} CCVerticalTextAlignment;
typedef enum
{
kCCTextAlignmentLeft,
kCCTextAlignmentCenter,
kCCTextAlignmentRight,
} CCTextAlignment;
S2,现在来看这个CCLabelTTF的一个小Demo。
//获取主屏幕的尺寸
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//创建标签
CCLabelTTF *label = CCLabelTTF::create("Hello CCLabelTTF", "Arial", 24);
//设置标签文字颜色
label->setColor(ccc3(0,0,0));
//设置标签位置
label->setPosition(ccp(winSize.width/2,winSize.height/2));
//设置锚点为左下角
label->setAnchorPoint(CCPointZero);
//设置标签的横向对齐方式为向左对齐,这样标签内容增加,只会向右增加
label->setHorizontalAlignment(kCCTextAlignmentLeft);
//添加为子节点
this->addChild(label,1);
这样设置对齐方式后,标签内容在修改,始终向左对齐,贴图如下: