原文地址:http://shahdza.blog.51cto.com/2410787/1560612
【唠叨】
在3.x中,废弃了2.x里的LabelTTF、LabelAtlas、LabelBMFont三个字体类,取而代之的是全新的字体标签Label。
实际上Label是将三个字体类进行了融合,进行统一的管理与渲染,这使得创建字体标签Label的方式更加统一,更加方便。
本节来学习一下3.x中新的标签类Label,如果对2.x中的三个字体类不了解的话,建议先去看看那三个类的用法,再来学习本节内容,能够更好的理解。
2.x中的旧标签类,请移步:http://www.jb51.cc/article/p-fdmvdxhh-wx.html
【致谢】
http://cn.cocos2d-x.org/tutorial/show?id=1446
http://www.cocoachina.com/bbs/read.php?tid=197179
【本节内容】
在3.x中,Label支持四种方式的标签创建。并新增了阴影Shadow、轮廓Outline、发光Glow效果的支持。还支持文字内容的行间距、文字间距、自动换行的设置。
>创建系统原生字体API:createWithSystemFont
创建TTF :createWithTTF (原LabelTTF)
创建CharMap :createWithCharMap (原LabelAtlas)
创建BMFont :createWithBMFont (原LabelBMFont)
Label的属性与方法
文字效果渲染 : Shadow、Outline、Glow
对齐方式 :TextHAlignment、TextVAlignment
Label的尺寸大小
自动换行
行间距、文字间距
单独设置某个字符
关于图片资源,请在cocos2dx给出的官方样例cpp-tests中寻找。
【createWithSystemFont】
创建系统原生字体的API。
创建方式如下:
1
2
3
4
5
6
7
8
9
10
|
static
Label*createWithSystemFont(
const
std::string&text,
std::string&font,0)!important; background:none!important">//字体(字体名称、或字体文件)
float
fontSize,0)!important; background:none!important">//字号
Size&dimensions=Size::ZERO,0)!important; background:none!important">//label的尺寸大小,默认不设置尺寸
TextHAlignmenthAlignment=TextHAlignment::LEFT,0)!important; background:none!important">//水平对齐方式,默认左对齐::LEFT
TextVAlignmentvAlignment=TextVAlignment::TOP
);
|
使用举例:
4
Label*lb1=Label::createWithSystemFont(
"123abc"
,
"Arial"
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,24);
|
Label*createWithTTF(
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
std::string&fontFile,0)!important; background:none!important">//必须为字体文件(如"*.ttf")
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
TextVAlignmentvAlignment=TextVAlignment::TOP
2、方式二:通过TTFConfig配置来创建
2.1、TTFConfig配置
10
11
12
13
14
15
16
17
18
19
20
21
typedef
struct
_ttfConfig
{
std::stringfontFilePath;
int
fontSize;
GlyphCollectionglyphs;
const
char
*customGlyphs;
bool
distanceFieldEnabled;
*filePath=
""
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
size=12,
GlyphCollection&glyphCollection=GlyphCollection::DYNAMIC,
*customGlyphCollection=nullptr,
usedistanceField=
false
ottom:auto!important; float:none!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
outline=0
);
|
TTFConfig&ttfConfig,0)!important; background:none!important">//TTFConfig配置
TextHAlignmentalignment=TextHAlignment::LEFT,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">maxlinewidth=0
);
|
TTFConfigttfConfig;
ttfConfig.fontFilePath=
"fonts/MarkerFelt.ttf"
;
ttfConfig.distanceFieldEnabled=
;
ttfConfig.glyphs=GlyphCollection::DYNAMIC;
ttfConfig.customGlyphs=nullptr;
Label*lb3=Label::createWithTTF(ttfConfig,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">);
|
//charMapFile:字符资源图片png
//itemWidth:每个字符的宽
//itemHeight:每个字符的高
//startCharMap:图片第一个是什么字符
Label*createWithCharMap(
std::string&charMapFile,
itemWidth,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">itemHeight,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">startCharMap);
Label*createWithCharMap(Texture2D*texture,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">startCharMap);
std::string&plistFile);
|
//create字符图片.png,每个字符宽,高,起始字符
Label*lb4=Label::createWithCharMap(
"fonts/digit.png"
'0'
);
lb4->setString(
"123456"
);
|
//创建图片纹理Texture2D
Texture2D*texture=TextureCache::getInstance()->addImage(
Label*lb5=Label::createWithCharMap(texture,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">);
lb5->setString(
//设置字符串内容
|
//
<?
xml
version
=
"1.0"
encoding
"UTF-8"
?>
<!DOCTYPEplistPUBLIC"-//Apple//DTdplIST1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<
plist
"1.0"
>
dict
>
key
>version</
>
integer
>1</
>
>textureFilename</
<!--字符图片资源:digit.png-->
string
>digit.png</
>
>itemWidth</
<!--每个字符的宽:20-->
>20</
>
>itemHeight</
<!--每个字符的高:20-->
>
>firstChar</
<!--起始字符:'0'-->
>48</
>
</
>
plist
>
//
|
使用plist创建CharMap的方法:
//plist的配置信息,如上所示
Label*lb6=Label::createWithCharMap(
"fonts/digit.plist"
lb6->setString(
);