1. 程式人生 > >Cocos2d—X遊戲開發之 CCLabelTTF 標籤詳解和對齊方式設定(分數顯示)(十六)

Cocos2d—X遊戲開發之 CCLabelTTF 標籤詳解和對齊方式設定(分數顯示)(十六)

在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);

這樣設定對齊方式後,標籤內容在修改,始終向左對齊,貼圖如下: