Cocos2d-x 3.0標簽類Label
Cocos2d-x 3.0後推出了新的標簽類Label這種標簽通過使用FreeType[1]來使它在不同的平台上有相同的視覺效果。由於使用更快的緩存代理它的渲染也將更加快速。Label提供了描邊和陰影等特性。
Label類的類圖如下圖所示

創建Label類靜態create函數常用的有如下幾個
static Label* createWithSystemFont(conststd::string &text, //是要顯示的文字
const std::string& font, //係統字體名
float fontSize, //字體的大小
const Size& dimensions = Size::ZERO, //在屏幕上占用的區域大小,可省略
TextHAlignment hAlignment = TextHAlignment::LEFT, //文字橫向對齊方式,可省略
TextVAlignment vAlignment = TextVAlignment::TOP) //文字縱向對齊方式,可省略
static Label* createWithTTF(conststd::string & text,
const std::string & fontFile, //字體文件
float fontSize,
const Size & dimensions = Size::ZERO, //可省略
TextHAlignment hAlignment= TextHAlignment::LEFT, //可省略
TextVAlignment vAlignment= TextVAlignment::TOP //可省略
)
static Label* createWithTTF(constTTFConfig& ttfConfig,
const std::string& text,
TextHAlignment alignment =TextHAlignment::LEFT,
int maxLineWidth = 0
)
static Label* createWithBMFont(conststd::string& bmfontFilePath, //位圖字體文件
const std::string& text,
const TextHAlignment& alignment =TextHAlignment::LEFT, //可省略
int maxLineWidth = 0, //可省略
const Point& imageOffset = Point::ZERO //可省略
)
其中createWithSystemFont是創建係統字體標簽對象createWithTTF是創建TTF字體標簽對象createWithBMFont是創建位圖字體標簽對象。
下麵我們通過一個實例介紹一下它們的使用。這個實例如圖下圖所示。

下麵我們看看HelloWorldScene.cpp中init函數如下
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x+ visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
autolabel1 = Label::createWithSystemFont("Hello World1","Arial", 36); ①
label1->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 100));
this->addChild(label1,1);
autolabel2 = Label::createWithTTF("Hello World2", "fonts/MarkerFelt.ttf", 36); ②
label2->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 200));
this->addChild(label2,1);
autolabel3 = Label::createWithBMFont("fonts/BMFont.fnt", "HelloWorld3"); ③
label3->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 300));
this->addChild(label3,1);
TTFConfigttfConfig("fonts/Marker Felt.ttf",
36,
GlyphCollection::DYNAMIC); ④
autolabel4 = Label::createWithTTF(ttfConfig, "Hello World4"); ⑤
label4->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 400));
this->addChild(label4, 1);
ttfConfig.outlineSize= 4; ⑥
autolabel5 = Label::createWithTTF(ttfConfig, "Hello World5"); ⑦
label5->setPosition(Point(origin.x+ visibleSize.width/2,
origin.y + visibleSize.height - 500));
label5->enableShadow(Color4B(255,255,255,128),Size(4, -4)); ⑧
label5->setColor(Color3B::RED); ⑨
this->addChild(label5,1);
return true;
}
在上麵的代碼中第①是通過createWithSystemFont函數創建Label對象第②行代碼是通過createWithTTF是創建TTF字體標簽對象第③行代碼是createWithBMFont是創建位圖字體標簽對象。
第④行代碼TTFConfig ttfConfig("fonts/Marker Felt.ttf", 36, GlyphCollection::DYNAMIC)是創建一個TTFConfig結構體變量TTFConfig結構體的定義如下
_ttfConfig(constchar* filePath = "", //字體文件路徑
int size = 12, //字體大小
constGlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, //字體庫類型
constchar * customGlyphCollection = nullptr, //自定義字體庫
booluseDistanceField = false, //用戶是否可縮放字體
intoutline = 0 //字體描邊
)
第⑤行代碼Label::createWithTTF(ttfConfig,"Hello World4")是通過指定TTFConfig創建TTF字體標簽。第⑥行代碼ttfConfig.outlineSize = 4設置TTFConfig的描邊字段。第⑦行代碼Label::createWithTTF(ttfConfig,"Hello World5")是重新創建TTF字體標簽。
第⑧行代碼label5->enableShadow(Color4B(255,255,255,128),Size(4, -4))是設置標簽的陰影效果。第⑨行代碼label5->setColor(Color3B::RED)是設置標簽的顏色。
[1] FreeType庫是一個完全免費開源的、高質量的且可移植的字體引擎它提供統一的接口來訪問多種字體格式文件。——引自於百度百科https://baike.baidu.com/view/4579855.htm
《Cocos2d-x實戰 C++卷》現已上線各大商店均已開售
京東https://item.jd.com/11584534.html
當當https://product.dangdang.com/23606265.html
互動出版網https://product.china-pub.com/3770734
《Cocos2d-x實戰 C++卷》源碼及樣章下載地址
源碼下載地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
樣章下載地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

最後更新:2017-04-03 12:56:36