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

Label类的类图如下图所示:

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

  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. static Label* createWithTTF(conststd::string & text,
  8. const std::string &  fontFile,                                                              //字体文件
  9. float fontSize,
  10. const Size &  dimensions = Size::ZERO,                                           //可省略
  11. TextHAlignment          hAlignment= TextHAlignment::LEFT,          //可省略
  12. TextVAlignment           vAlignment= TextVAlignment::TOP              //可省略
  13. )
  14. static Label* createWithTTF(constTTFConfig& ttfConfig,
  15. const std::string& text,
  16. TextHAlignment alignment =TextHAlignment::LEFT,
  17. int maxLineWidth = 0
  18. )
  19. static Label* createWithBMFont(conststd::string& bmfontFilePath,          //位图字体文件
  20. const std::string&  text,
  21. const TextHAlignment& alignment =TextHAlignment::LEFT, //可省略
  22. int maxLineWidth = 0,                                                                       //可省略
  23. const Point&  imageOffset = Point::ZERO                                //可省略
  24. )

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

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

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

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

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

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

  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

Cocos2d-x 3.0标签类Label的更多相关文章

  1. Flutter 标签类控件大全Chip

    老孟导读:Flutter内置了多个标签类控件,但本质上它们都是同一个控件,只不过是属性参数不同而已,在学习的过程中可以将其放在放在一起学习,方便记忆. RawChip Material风格标签控件,此 ...

  2. JavaEE自定义标签:标签类的创建、tld配置文件的创建(位置、如何创建)、Web-XML配置、JSP应用

    1.标签 以类似于html标签的方式实现的java代码的封装. 第一:形成了开发标签的技术标准---自定义标签的技术标准. 第二:java标准标签库(sun之前自己开发的一系列的标签的集合)jstl, ...

  3. 小tips:JS语法之标签(label)

    JavaScript语言允许,语句的前面有标签(label),相当于定位符,用于跳转到程序的任意位置,标签的格式如下. label: statement 标签可以是任意的标识符,但是不能是保留字,语句 ...

  4. EffectiveJava(20)使用子类型化优化标签类

    标签类:其中有许多样板代码,包括枚举声明,标签域和条件语句 如果要给它添加风格,除了有权限修改源码之外,你还得给每个条件语句都添加一个条件,否则就会在运行时失败 标签类过于冗长,容易出错,并且效率低下 ...

  5. 列表框、分组列表框、标签(label)、分组框(fieldset)、框架(frameset)

    列表框(select) 下拉列表,用户可以从一些可选项中选择. 示例:简单的下拉列表 <select name="country"> <option value= ...

  6. EF 5.0 帮助类

    EF 5.0 帮助类 加入命名空间: using System; using System.Data; using System.Data.Entity; using System.Data.Enti ...

  7. EffectiveJava——类层次优于标签类

    标签类: 有时候,可能会遇到带有两种甚至更多钟风格的类的实例的类,并包含表示实例风格的(tag)域.例如下面这个类,它能够表示圆形或者矩形: /** * 类层次优先与标签类 * @author wei ...

  8. EF 5.0 帮助类 增删改查

    原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...

  9. Aspose.Cells for .NET 8.5.0 工具类

    基于 Aspose.Cells for .NET 8.5.0 工具类, Aspose.Cells for .NET 8.5.0 这个自己去CSDN下载里面有破解的,没有破解的导出excel的时候会(A ...

随机推荐

  1. 绝对好文:.NET程序性能的基本要领

    Bill Chiles(Roslyn编译器的程序经理)写了一篇文章<Essential Performance Facts and .NET Framework Tips>,知名博主寒江独 ...

  2. [Express] Level 4: Body-parser -- Delete

    Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? H ...

  3. Android记录一个setTextColor常见的一个bug

    今天写代码 一不小心就犯了个错误. 细致检查才发现,仅记录一下,防止各位同学犯相同的错误哦 代码例如以下: remote.setTextColor(summaryId, R.color.news_ha ...

  4. 了解javascript中的this --实例篇

    对javascript this的赋值有了深一层的理解后,看一下比较复杂的情况,this的应用篇参考<对javascript this的理解>. #demo1 var name=" ...

  5. IOS中类和对象还有,nil/Nil/NULL的区别

    转自:http://blog.sina.com.cn/s/blog_5fb39f910101akm1.html 类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性 ...

  6. Tomcat7启动报Error listenerStart错误--转载

    原文地址:http://www.cnblogs.com/nayitian/p/3439336.html 问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 ...

  7. 修改FFMpeg源码—捕获丢包

    概述 最近我们项目有一个需求就是解决客户端播放RTSP视频流花屏的问题,一般来说丢包就会引起花屏,导致客户端花屏的因素又有很多,比如说: 相机到服务器丢包 服务器到客户端丢包 等等... 其中服务器到 ...

  8. LeetCode31 Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  9. magic_quotes_gpc(魔术引号开关)

    magic_quotes_gpc函数在php中的作用是判断解析用户提示的数据,如包括有:post.get.cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序,特别是数据库语句因为特殊 ...

  10. 3.1html学习之列表

    一.含义: ul:unorder list ol:order list li:list item dl:definition list dt:definition term dd:definition ...