New Label of Cocos2d-x v3.0
- New Label of Cocos2d-x v3.0
- What's new of Label in Cocos2d-x v3.0?
- How to use new Label?
- Create with SystemFont
- Create with TTF
- Create with BMFont
- Create with CharMap
- Glow,shadow and outline effects
What's new of Label in Cocos2d-x v3.0?
- Deprecated
CCLabelTTF ,CCLabelBMFont ,CCLabelAtlas . There is only one classLabel since Cocos2d-x v3.0.
- Glow, shadow and outline effects supported.
- Using
freetype2 to generate texture for labels,which improve the speed of creating fonts and make sure that labels have the same effect on different platforms.
How to use new Label?
Create with SystemFont
createWithSystemFont() will call the native API by the platform-dependent code. The first 3 parameters are necessaries and the rest would have a default value if they were omitted.
1
2
3
|
static Label* createWithSystemFont(const std::string& text,const std::string& font,float fontSize,const Size& dimensions = Size::ZERO,TextHAlignment hAlignment = TextHAlignment::LEFT,TextVAlignment vAlignment = TextVAlignment::TOP);
|
Create with TTF
CreateWithTTF() uses libfreetype2 to create the fonts. It would save each char of the strings to the cache that improve the speed of creating fonts. There are two ways to create a LabelTTF bellows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
auto label = Label::createWithTTF("label test","fonts/Marker Felt.ttf",32);
label->setPosition(Point(size.width/2,size.height*0.6));
this->addChild(label);
TTFConfig label_config;
label_config.fontFilePath = "fonts/Marker Felt.ttf";
label_config.fontSize = 32;
label_config.glyphs = GlyphCollection::DYNAMIC;
label_config.customGlyphs = nullptr;
label_config.distanceFieldEnabled = false;
label_config.outlineSize = 0;
auto label_two = Label::createWithTTF(label_config,"label test");
label_two->setPosition(Point(size.width/2,size.height*0.5));
this->addChild(label_two);
|
To know what is distanceField , read this paper Distance-field-fonts
Create with BMFont
To create a label with FNT file,we have createWithBMFont()
1
2
3
|
auto bmfont = Label::createWithBMFont("fonts/gameover_score_num.fnt","123456789");
|
Create with CharMap
The createWithCharMap() represents the CCLabelAtlas in the past. It has a simpler rule compare to the BMFont. It is common used in the number fonts. We have 3 overrided functions in Cocos2d-x v3.0 above to create with char map.
//创建charMap 参数分别为:png图片的路径,每个字符的宽和高,起始字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
static Label * createWithCharMap(const std::string& charMapFile,int itemWidth,int itemHeight,int startCharMap);
static Label * createWithCharMap(Texture2D* texture,int startCharMap);
static Label * createWithCharMap(const std::string& plistFile);
auto charMap = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.png",48,64,' ');
charMap->setPosition(Point(size.width/2,size.height*0.4));
charMap->setString("123456789");
this->addChild(charMap);
auto charMap2 = Label::createWithCharMap("fonts/tuffy_bold_italic-charmap.plist");
charMap2->setPosition(Point(Point(size.width/2,size.height*0.3)));
charMap2->setString("123456789");
this->addChild(charMap2);
|
Glow,shadow and outline effects
1
2
3
4
5
6
7
8
|
label_two->enableOutline(Color4B(255,0,255),5);
label_three->enableGlow(Color4B(255,255));
label_four->enableShadow(Color4B(0,255,Size(3,10),0);
|
distance-field-fonts.png(34.7 kB) zijian.rao,2014-07-29 09:26
2.png(30.9 kB) zijian.rao,2014-07-29 09:26
1.png(35.4 kB) zijian.rao,2014-07-29 09:26
3.png(52.4 kB) zijian.rao,2014-07-29 09:26
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|