Label類的類圖以下圖所示:html
建立Label類靜態create函數經常使用的有以下幾個:緩存
[html] view plaincopy函數
static Label* createWithSystemFont(conststd::string &text, //是要顯示的文字 字體
const std::string& font, //系統字體名 this
float fontSize, //字體的大小 spa
const Size& dimensions = Size::ZERO, //在屏幕上佔用的區域大小,可省略 .net
TextHAlignment hAlignment = TextHAlignment::LEFT, //文字橫向對齊方式,可省略 代理
TextVAlignment vAlignment = TextVAlignment::TOP) //文字縱向對齊方式,可省略 code
static Label* createWithTTF(conststd::string & text, orm
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函數以下:
[html] view plaincopy
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結構體的定義以下:
[html] view plaincopy
_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庫是一個徹底免費(開源)的、高質量的且可移植的字體引擎,它提供統一的接口來訪問多種字體格式文件。——引自於百度百科http://baike.baidu.com/view/4579855.htm