您好,登錄后才能下訂單哦!
Label類的類圖如下圖所示:
創(chuàng)建Label類靜態(tài)create函數(shù)常用的有如下幾個(gè):
static Label* createWithSystemFont(conststd::string &text, //是要顯示的文字 const std::string& font, //系統(tǒng)字體名 float fontSize, //字體的大小 const Size& dimensions = Size::ZERO, //在屏幕上占用的區(qū)域大小,可省略 TextHAlignment hAlignment = TextHAlignment::LEFT, //文字橫向?qū)R方式,可省略 TextVAlignment vAlignment = TextVAlignment::TOP) //文字縱向?qū)R方式,可省略 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& p_w_picpathOffset = Point::ZERO //可省略 )
其中createWithSystemFont是創(chuàng)建系統(tǒng)字體標(biāo)簽對(duì)象,createWithTTF是創(chuàng)建TTF字體標(biāo)簽對(duì)象,createWithBMFont是創(chuàng)建位圖字體標(biāo)簽對(duì)象。
下面我們通過一個(gè)實(shí)例介紹一下,它們的使用。這個(gè)實(shí)例如圖下圖所示。
下面我們看看HelloWorldScene.cpp中init函數(shù)如下:
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函數(shù)創(chuàng)建Label對(duì)象,第②行代碼是通過createWithTTF是創(chuàng)建TTF字體標(biāo)簽對(duì)象,第③行代碼是createWithBMFont是創(chuàng)建位圖字體標(biāo)簽對(duì)象。
第④行代碼TTFConfig ttfConfig("fonts/Marker Felt.ttf", 36, GlyphCollection::DYNAMIC)是創(chuàng)建一個(gè)TTFConfig結(jié)構(gòu)體變量,TTFConfig結(jié)構(gòu)體的定義如下:
_ttfConfig(constchar* filePath = "", //字體文件路徑 int size = 12, //字體大小 constGlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, //字體庫類型 constchar * customGlyphCollection = nullptr, //自定義字體庫 booluseDistanceField = false, //用戶是否可縮放字體 intoutline = 0 //字體描邊 )
第⑤行代碼Label::createWithTTF(ttfConfig,"Hello World4")是通過指定TTFConfig創(chuàng)建TTF字體標(biāo)簽。第⑥行代碼ttfConfig.outlineSize = 4設(shè)置TTFConfig的描邊字段。第⑦行代碼Label::createWithTTF(ttfConfig,"Hello World5")是重新創(chuàng)建TTF字體標(biāo)簽。
第⑧行代碼label5->enableShadow(Color4B(255,255,255,128),Size(4, -4))是設(shè)置標(biāo)簽的陰影效果。第⑨行代碼label5->setColor(Color3B::RED)是設(shè)置標(biāo)簽的顏色。
更多精彩內(nèi)容請(qǐng)關(guān)注史上最牛的cocos2d-x課程
《cocos2d-x手機(jī)游戲開發(fā)實(shí)戰(zhàn)》直播課程第一期
報(bào)名Cocos2d-x直播課程還送關(guān)東升老師iOS終身會(huì)員。
課程鏈接:http://edu.51cto.com/pack/view/id-13.html
會(huì)員鏈接:http://edu.51cto.com/member/id-3.html
更多內(nèi)容請(qǐng)關(guān)注Cocos2d-x系列圖書《Cocos2d-x實(shí)戰(zhàn)(卷Ⅰ):C++開發(fā)》
本書交流討論網(wǎng)站:http://www.cocoagame.net
歡迎加入cocos2d-x技術(shù)討論群:257760386、327403678
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。