加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2dx 3.0 -- 有freeType做靠山的Label

发布时间:2020-12-14 16:27:09 所属栏目:百科 来源:网络整理
导读:cocos2dx 3.0版本之前,我们一直都是用CCLabelTTF,CCLabelBMFont,CCLabelAtlas来创建文本标签, 但是!3.0版本放出来后... 看到这里你心里是不是又颤抖了一下?别害怕嘛,我要说的是: 3.0版本出来后这些标签也都是还可以用的啦,只是说我们有了更好的选择

cocos2dx 3.0版本之前,我们一直都是用CCLabelTTF,CCLabelBMFont,CCLabelAtlas来创建文本标签,但是!3.0版本放出来后...看到这里你心里是不是又颤抖了一下?别害怕嘛,我要说的是:3.0版本出来后这些标签也都是还可以用的啦,只是说我们有了更好的选择。


cocos2dx3.0添加了一种新的文本标签,这种标签不同的地方有: 使用freetype来使它在不同的平台上有相同的视觉效果;由于使用更快的缓存代理,它的渲染也将更加快速;同时它还提供了绘边、阴影等特性。

所以因为Label,我决定离开LabelTTF和LabelBMFont(这个开头你猜到了么?)
---------------------------------------------------
常用的接口一览(因为很多接口都与LabelTTFT等一样,所以就列一些我所了解的“异类”)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//创建普通的文本标签,效果和CCLabelTTF::create(...);一样。TTFConfig是什么?下面会介绍
static Label * create( const std::string& text, std::string& fontName, float fontSize,
Size& dimensions = Size::ZERO,TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP);
//通过读取TTFConfig配置的方式创建标签,
Label* createWithTTF( TTFConfig& ttfConfig,TextHAlignment alignment = TextHAlignment::LEFT,monospace!important; font-weight:bold!important; font-size:1em!important; min-height:inherit!important; color:gray!important; background:none!important">int lineWidth = 0);
//使用.fnt的方式创建标签,类似CCLabelBMFont:create();
Label* createWithBMFont( std::string& bmfontFilePath,
TextHAlignment& alignment = TextHAlignment::LEFT,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> lineWidth = 0,
Point& imageOffset = Point::ZERO);
//使用.png的方式创建标签,类似CCLabelAtlas::create();
Label * createWithCharMap( std::string& charMapFile,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> itemWidth,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> itemHeight,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> startCharMap);
virtual void enableShadow( Color3B& shadowColor = Color3B::BLACK,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> Size &offset = Size(2,-2),monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> opacity = 0.75f,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> blurRadius = 0);
enableOutline( Color4B& outlineColor,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important"> outlineSize = -1); //只支持TTF
enableGlow( Color3B& glowColor); //只支持 TTF
disableEffect(); //取消所有特效
//特效的种类有一下四种:
enum class LabelEffect {
NORMAL, //普通标签(纯粹的、脱离了低级趣味的label)
OUTLINE,0)!important; background:none!important">//文艺标签(有描边)
SHADOW,0)!important; background:none!important">//2B标签 (有阴影)
GLOW //土豪标签(有荧光)
};
稍微提一下一个新东西:TTFConfig
?
22
//TTFConfig 是一个结构体,里面包含了你要创建一个ttf的label常用配置,如下所示
typedef struct _ttfConfig
{
std::string fontFilePath; //文件路径
fontSize; //字体大小,默认12
GlyphCollection glyphs; //使用的字符集,默认DYNAMIC
const char *customGlyphs; //呵呵
bool distanceFieldEnabled; //我对这个的理解是:是否让文字显得紧凑?默认为false
outlineSize; //字体描边的大小,默认为0
//构造函数
...
//注意:当outlineSize初始化的值大于0时,distanceFieldEnabled则为false
}TTFConfig;
//GlyphCollection有四种类型:
GlyphCollection {
DYNAMIC,
NEHE,
ASCII,
CUSTOM
};
下面简单介绍Label的用法

1、使用.ttf
1)创建


复制代码
  1. TTFConfig config2("fonts/Marker Felt.ttf",30);//初始化TTFConfig,第一个参数为字库的路径,第二个参数为字体大小
  2. auto label2 = Label::createWithTTF(config2,"createWithTTF",TextHAlignment::LEFT);//创建label,并向左对其
  3. label2->setPosition(Point(visibleSize.width/2,300));
  4. label2->setAnchorPoint(Point::ANCHOR_MIDDLE);//设置锚点居中
  5. this->addChild(label2,2);

当然了,也可以用Label创建普通的标签,效果和用CCLabelTTF::create()的一样
复制代码
auto label4 = Label::create("create","Marker Felt",30);//
效果如图所示:

2)另字体看起来紧凑点,也就是设置distanceFieldEnabled = true
直接修改config里的distanceFieldEnabled,方式如下:
复制代码

3)设置glow(荧光)效果,(我也不知道该怎么描述glow这词...)

复制代码
label2->enableGlow(Color3B::GREEN);//荧光颜色为绿色
效果如图所示。这里有个地方要注意下, 想要显示荧光效果,必须令distanceFieldEnabled = true,否则看不到效果。

4)设置描边
复制代码
label2->enableOutline(Color4B(255,125,255),8);//第一个参数为描边的颜色,第二个参数为描边的大小

效果如图所示。注意, 使用描边效果后,distanceFieldEnabled 将变成 false,这也意味着在有描边的情况下是显示不了荧光效果的(我想太多了...)

5)设置阴影
复制代码
label2->enableShadow(Color3B::RED,Size(2,0.2,0.5);//第一个参数为阴影颜色,第二个参数为阴影相对于标签的坐标,第三个参数设置透明度,第四个参数与模糊有关


2、使用.fnt 的label
1)创建

复制代码
auto label3 = Label::createWithBMFont("fonts/bitmapFontTest.fnt","createWithBMFont");
  • label3->setPosition(Point(visibleSize.width/2,250));
  • label3->setAnchorPoint(Point::ANCHOR_MIDDLE);
  • this->addChild(label3,2);
  • label3->enableShadow();



  • 2)设置阴影(描边和荧光只能用在.ttf 上)
    复制代码
    label3->enableShadow(Color3B::RED);

    效果如图,可以与上图对比一下。


    3、使用.png
    加入我们有这么一张图,使用方法如下:


    1)创建
    复制代码
    auto label4= Label::createWithCharMap("fonts/costFont.png",44,'/');//参数分别为:路径;每个字符的宽和高,起始字符
  • label4->setPosition(Point(visibleSize.width/2,200));
  • label4->setAnchorPoint(Point::ANCHOR_MIDDLE);
  • label4->setString("10");//设置显示的内容为”10“this->addChild(label4,2);



  • 2)设置阴影
    复制代码
    label4->enableShadow(Color3B::RED);



    4、取消所有特效
    复制代码
    label->disableEffect();//取消所有特效
    恩,就介绍到这里。具体的用法可以参考testCpp。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!