Cocos2d-x 3.0标签类Label

Cocos2d-x 3.0后推出了新的标签类Label,这种标签通过使用FreeType[1]来使它在不同的平台上有相同的视觉效果。由于使用更快的缓存代理,它的渲染也将更加快速。Label提供了描边和阴影等特性。

Label类的类图如下图所示:

Cocos2d-x 3.0标签类Label_第1张图片

 

创建Label类静态create函数常用的有如下几个:

static Label* createWithSystemFont(conststd::string &text,             //是要显示的文字                           
                  const std::string& font,                                                       //系统字体名
                  float fontSize,                                                            //字体的大小
                  const Size& dimensions = Size::ZERO,                            //在屏幕上占用的区域大小,可省略
                  TextHAlignment  hAlignment = TextHAlignment::LEFT,          //文字横向对齐方式,可省略
                  TextVAlignment  vAlignment = TextVAlignment::TOP)   //文字纵向对齐方式,可省略
 
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&  imageOffset = Point::ZERO                                //可省略
    ) 

其中createWithSystemFont是创建系统字体标签对象,createWithTTF是创建TTF字体标签对象,createWithBMFont是创建位图字体标签对象。

下面我们通过一个实例介绍一下,它们的使用。这个实例如图下图所示。

Cocos2d-x 3.0标签类Label_第2张图片

下面我们看看HelloWorldScene.cppinit函数如下:

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结构体的定义如下:

              

_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

 


更多内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
本书交流讨论网站: http://www.cocoagame.net
更多精彩视频课程请关注智捷课堂Cocos课程: http://v.51work6.com
欢迎加入Cocos2d-x技术讨论群:257760386


《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:

京东:http://item.jd.com/11584534.html

亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当:http://product.dangdang.com/23606265.html

互动出版网:http://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源码及样章下载地址:

源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台
Cocos2d-x 3.0标签类Label_第3张图片

你可能感兴趣的:(游戏,C++,cocos2d,移动开发,cocos2d-x)