Cocos2d-x 3.0标签类Label

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

Label类的类图如下图所示:

Cocos2d-x 3.0标签类Label

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

[html] view plaincopyCocos2d-x 3.0标签类LabelCocos2d-x 3.0标签类Label
 
  1. static Label* createWithSystemFont(conststd::string &text,             //是要显示的文字                             
  2.                   const std::string& font,                                                       //系统字体名  
  3.                   float fontSize,                                                            //字体的大小  
  4.                   const Size& dimensions = Size::ZERO,                            //在屏幕上占用的区域大小,可省略  
  5.                   TextHAlignment  hAlignment = TextHAlignment::LEFT,          //文字横向对齐方式,可省略  
  6.                   TextVAlignment  vAlignment = TextVAlignment::TOP)   //文字纵向对齐方式,可省略  
  7.    
  8. static Label* createWithTTF(conststd::string & text,  
  9.          const std::string &  fontFile,                                                              //字体文件  
  10.          float fontSize,  
  11.          const Size &  dimensions = Size::ZERO,                                           //可省略  
  12.          TextHAlignment          hAlignment= TextHAlignment::LEFT,          //可省略  
  13.          TextVAlignment           vAlignment= TextVAlignment::TOP              //可省略  
  14.     )       
  15.    
  16. static Label* createWithTTF(constTTFConfig& ttfConfig,  
  17.          const std::string& text,  
  18.          TextHAlignment alignment =TextHAlignment::LEFT,  
  19.          int maxLineWidth = 0  
  20.     )  
  21.    
  22. static Label* createWithBMFont(conststd::string& bmfontFilePath,          //位图字体文件  
  23.          const std::string&  text,                                                              
  24.          const TextHAlignment& alignment =TextHAlignment::LEFT, //可省略  
  25.          int maxLineWidth = 0,                                                                       //可省略  
  26.          const Point&  imageOffset = Point::ZERO                                //可省略  
  27.     )   

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

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

Cocos2d-x 3.0标签类Label

下面我们看看HelloWorldScene.cpp中init函数如下:

[html] view plaincopyCocos2d-x 3.0标签类LabelCocos2d-x 3.0标签类Label
 
  1. bool HelloWorld::init()  
  2. {  
  3.    if ( !Layer::init() )  
  4.    {  
  5.        return false;  
  6.    }  
  7.      
  8.    Size visibleSize = Director::getInstance()->getVisibleSize();  
  9.    Point origin = Director::getInstance()->getVisibleOrigin();  
  10.    auto closeItem = MenuItemImage::create(  
  11.                                           "CloseNormal.png",  
  12.                                           "CloseSelected.png",  
  13.                                  CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));  
  14.      
  15.     closeItem->setPosition(Point(origin.x+ visibleSize.width - closeItem->getContentSize().width/2 ,  
  16.                                 origin.y + closeItem->getContentSize().height/2));  
  17.    
  18.     
  19.    auto menu = Menu::create(closeItem, NULL);  
  20.    menu->setPosition(Point::ZERO);  
  21.    this->addChild(menu, 1);  
  22.      
  23.     autolabel1 = Label::createWithSystemFont("Hello World1","Arial", 36);                                   ①  
  24.     label1->setPosition(Point(origin.x+ visibleSize.width/2,  
  25.          origin.y + visibleSize.height - 100));  
  26.     this->addChild(label1,1);  
  27.    
  28.     autolabel2 = Label::createWithTTF("Hello World2", "fonts/MarkerFelt.ttf", 36);                       ②  
  29.     label2->setPosition(Point(origin.x+ visibleSize.width/2,  
  30.          origin.y + visibleSize.height - 200));  
  31.     this->addChild(label2,1);  
  32.    
  33.     autolabel3 = Label::createWithBMFont("fonts/BMFont.fnt", "HelloWorld3");                            ③  
  34.     label3->setPosition(Point(origin.x+ visibleSize.width/2,  
  35.          origin.y + visibleSize.height - 300));  
  36.     this->addChild(label3,1);  
  37.    
  38.     TTFConfigttfConfig("fonts/Marker Felt.ttf",  
  39.          36,  
  40.          GlyphCollection::DYNAMIC);                                                                                                  ④  
  41.     autolabel4 = Label::createWithTTF(ttfConfig, "Hello World4");                                                  ⑤  
  42.     label4->setPosition(Point(origin.x+ visibleSize.width/2,  
  43.          origin.y + visibleSize.height - 400));  
  44.     this->addChild(label4, 1);  
  45.    
  46.     ttfConfig.outlineSize= 4;                                                                                                     ⑥  
  47.     autolabel5 = Label::createWithTTF(ttfConfig, "Hello World5");                                                  ⑦  
  48.     label5->setPosition(Point(origin.x+ visibleSize.width/2,  
  49.          origin.y + visibleSize.height - 500));  
  50.     label5->enableShadow(Color4B(255,255,255,128),Size(4, -4));                                        ⑧  
  51.     label5->setColor(Color3B::RED);                                                                                                 ⑨  
  52.     this->addChild(label5,1);  
  53.    
  54.  return true;  
  55.               }  

在上面的代码中第①是通过createWithSystemFont函数创建Label对象,第②行代码是通过createWithTTF是创建TTF字体标签对象,第③行代码是createWithBMFont是创建位图字体标签对象。

第④行代码TTFConfig ttfConfig("fonts/Marker Felt.ttf", 36, GlyphCollection::DYNAMIC)是创建一个TTFConfig结构体变量,TTFConfig结构体的定义如下:

              

[html] view plaincopyCocos2d-x 3.0标签类LabelCocos2d-x 3.0标签类Label
 
  1. _ttfConfig(constchar* filePath = "",                                                                         //字体文件路径  
  2.     int  size = 12,                                                                                            //字体大小  
  3.     constGlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,     //字体库类型  
  4.     constchar * customGlyphCollection = nullptr,                                     //自定义字体库  
  5.     booluseDistanceField = false,                                                                         //用户是否可缩放字体  
  6.     intoutline = 0                                                                                                      //字体描边  
  7.                )  

第⑤行代码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

相关推荐