前言:

介绍cocos2d-x中 标签类。

cocos2d-x中 标签类 主要有三种:LabelTTF, LabelAtlas, 和 LabelBMFont。此外,在Cocos2d-x 3.x之后推出了 新的标签类 Label。

LabelTTF 标签类

LabelTTF是使用系统中的字体,它是最简单的标签类。

create 静态函数完整定义:(此处只介绍一种常用的 create 静态方法)

 static LabelTTF * create(const std::string& string,
       const std::string& fontName,
       float fontSize,
       const Size& dimensions = Size::ZERO,
       TextHAlignment hAlignment = TextHAlignment::CENTER,
       TextVAlignment vAlignment = TextVAlignment::TOP);

create函数的第1个参数是要显示的文字,第2个参数是系统字体名,第3个参数是字体的大小。后面省略了三个参数。

示例:

     /////////// 4.2.1  LabelTTF
     LabelTTF* labelOne = LabelTTF::create();
     labelOne->setPosition(Point(origin.x + visibleSize.width / 2,     //// 字体居中显示
         origin.y + visibleSize.height - labelOne->getContentSize().height));
     );

LabelAtlas 标签类

LabelAtlas 是图片集标签,其中的 Atlas 本意是“地图集”、“图片集”,这种标签显示的文字是从一个图片集中取出的,因此使用LabelAtlas需要额外加载图片集文件。

LabelAtlas 比 LabelTTF快很多。

LabelAtlas 中的每个字符必须有固定的高度和宽度。

create 静态函数完整定义:

     static LabelAtlas* create(const std::string& string,
         const std::string& charMapFile,
         int itemWidth,
         int itemHeight,
         int startCharMap);

create 函数的第1个参数是要显示的文字,第2个参数是图片集文件,第3个参数是字符宽度,第4个参数是字符高度,第5个参数是开始字符。

示例:

     ///////////// 4.2.2  LabelAtlas
     //创建 并初始化标签
     auto labelTwo = LabelAtlas::create(, , ' ');
     labelTwo->setPosition(Point(origin.x + visibleSize.width / 2 - labelTwo->getContentSize().width/2,  //// 字体居中显示
         origin.y + visibleSize.height - labelTwo->getContentSize().height * ));
 //    labelTwo->setAnchorPoint(ccp(0.5, 0.5));
     );

注意:在 LabelAtlas 创建在字体中,anchorPoint默认好像是ccp(0,0) 的。你可以创建 LabelAtlas 字体 验证验证。

LabelBMFont 标签类

LabelBMFont是位图字体标签,需要添加字体文件:包括一个图片集(.png)和一个字符坐标文件(.fnt)。

LabelBMFont 比 LabelTTF 快很多。

LabelBMFont 中的每个字符的宽度是可变的。

create 静态函数完整定义:

     static LabelBMFont * create(const std::string& str,
         const std::string& fntFile,
         ,
         TextHAlignment alignment = TextHAlignment::LEFT,
         const Point& imageOffset = Point::ZERO);

create 函数第1个参数是要显示的文字,第2个参数是图片集文件(.fnt)。

示例:

     ///// 4.2.3   LabelBMFont
     auto labelThree = LabelBMFont::create("hello LabelBMFont", "Demo3/BMFont.fnt");
     labelThree->setPosition(Point(origin.x + visibleSize.width / 2 ,
         origin.y + visibleSize.height - labelThree->getContentSize().height * ));
     );

Cocos2d-x 3.x 标签类 Label

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

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

createWithSystemFont 示例:

通过 createWithSystemFont 函数创建 Label 对象

     auto label1 = Label::createWithSystemFont();
     label1->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - label1->getContentSize().height));
     );
     static Label * createWithTTF(const std::string& text,
         const std::string& fontFile,   /// 字体文件
         float fontSize,
         const Size& dimensions = Size::ZERO,
         TextHAlignment hAlignment = TextHAlignment::LEFT,
         TextVAlignment vAlignment = TextVAlignment::TOP);

createWithTTF 示例:

通过 createWithTTF 创建 TTF 字体标签对象

     auto label2 = Label::createWithTTF();
     label2->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );
     static Label* createWithTTF(const TTFConfig& ttfConfig,
         const std::string& text,
         TextHAlignment alignment = TextHAlignment::LEFT,
         );

createWithTTF 示例:

通过 指定的 TTFConfig 创建 TTF 字体标签对象

     TTFConfig ttfConfig(, GlyphCollection::DYNAMIC);
     auto label3 = Label::createWithTTF(ttfConfig, "Hello Label3");
     label3->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );
     ttfConfig.outlineSize = ;
     auto label4 = Label::createWithTTF(ttfConfig, "hello Label4");
     label4->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     label4->, , , ), Size(, -));
     label4->setColor(Color3B::RED);
     );
     static Label* createWithBMFont(const std::string& bmfontFilePath,
         const std::string& text,
         const TextHAlignment& alignment = TextHAlignment::LEFT,
         ,
         const Point& imageOffset = Point::ZERO);

示例:

通过 createWithBMFont 创建位图字体标签对象

     auto label5 = Label::createWithBMFont("Demo3/BMFont.fnt", "Hello Label5");
     label5->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );

补充说明:

Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

标签中文乱码问题:

     auto testLabel1 = LabelTTF::create();
     testLabel1->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );

     auto testLabel2 = LabelBMFont::create("中国2", "bitmapFontChinese.fnt");
     testLabel2->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );

     auto testLabel3 = Label::createWithBMFont("bitmapFontChinese.fnt", "中国3");
     testLabel3->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );

     TTFConfig ttfConfig(, GlyphCollection::DYNAMIC);
     auto testLabel4 = Label::createWithTTF(ttfConfig, "晚上好!", TextHAlignment::CENTER, visibleSize.width);
     testLabel4->setPosition(Point(origin.x + visibleSize.width / , origin.y + visibleSize.height - ));
     );

在windows中文环境下使用Visual Studio创建的源程序文件是采用 GBK 编码。

解决方法:

方法1:在Visual Studio工具中选择【文件】-->【高级保存】,选择【Unicode(UTF-8无签名) - 代码页 65001】,单击确定按钮保存。

方法2:用记事本打开文件,另存为 UTF-8 。

cocos2d-x实战 C++卷 学习笔记--第4章 使用标签的更多相关文章

  1. cocos2d-x实战 C++卷 学习笔记--第4章 字符串 __String类

    前言: <cocos2d-x实战C++卷>学习笔记.(cocos2d-x 是3.0版本) 介绍 cocos2d-x 通用的字符串类  __String . 使用cocos2d::__Str ...

  2. cocos2d-x实战 C++卷 学习笔记--第7章 动作、特效(一)

    前言: 介绍cocos2d-x中的动作.特效. 动作: 动作(action)包括基本动作和基本动作的组合,这些基本动作有缩放.移动.旋转等,而且这些动作变化的速度也可以设定. 动作类是 Action. ...

  3. cocos2d-x实战 C++卷 学习笔记--第6章 场景与层

    前言: 一个场景(Scene)是由多个层(Layer)组成,而且层的个数要至少是1,不能为0. 场景切换 场景切换相关函数 1)void  runWithScene(Scene*  scene) 该函 ...

  4. cocos2d-x实战 C++卷 学习笔记--第4章 使用菜单

    前言: 菜单中包含菜单项,菜单项类是 MenuItem ,每个菜单项都有三个基本状态:正常.选中和禁止. (MenuItem)菜单分类: (文本菜单)MenuItemLabel : MenuItemA ...

  5. cocos2d-x实战 C++卷 学习笔记--第4章 win32平台下中文乱码问题

    前言: 将GBK编码的字符串转为UTF-8编码.(通俗点说就是解决中文乱码问题) 简要介绍: 在Win32平台下通过 log 输出中文字符时,会出现中文乱码问题.同样的代码在 ios 和 Androi ...

  6. cocos2d-x实战 C++卷 学习笔记--第5章 精灵

    前言: 精灵类是Sprite类.它的子类有PhysicsSprite 和 Skin. PhysicsSprite 是物理引擎精灵类,而Skin是皮肤精灵类,用于骨骼动画. 创建Sprite精灵对象 创 ...

  7. Docker技术入门与实战 第二版-学习笔记-10-Docker Machine 项目-2-driver

    1>使用的driver 1〉generic 使用带有SSH的现有VM/主机创建机器. 如果你使用的是机器不直接支持的provider,或者希望导入现有主机以允许Docker Machine进行管 ...

  8. Docker技术入门与实战 第二版-学习笔记-8-网络功能network-3-容器访问控制和自定义网桥

    1)容器访问控制 容器的访问控制,主要通过 Linux 上的 iptables防火墙来进行管理和实现. iptables是 Linux 上默认的防火墙软件,在大部分发行版中都自带. 容器访问外部网络 ...

  9. Spring实战第八章学习笔记————使用Spring Web Flow

    Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...

随机推荐

  1. UIScrollView的属性总结

    contentSize是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下 ...

  2. unable to load default svn client myeclipse SVN安装,wen7 64位安装SVN

    在安装完后连接svn时出现unable to load default svn client的错误提示,百度知道是版本不对,我安装的是1.8的版本,插件按成1.6的了,只需下载1.8插件安装就行了 安 ...

  3. 如何检查机器是否因为装了Windows更新而需要重新启动

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何检查机器是否因为装了Windows更新而需要重新启动.

  4. 我眼中的PM

    我眼中的PM 1 我眼中的PM 1.1 人云“一个管理,半个专家”,我说“一个管理,两个专家” 如今,我发现我们不得不面对这样一个现实——角色兼职.我习惯上把项目分为三类:性命攸关的项目(涉及到人身安 ...

  5. Finite Difference Method with Mathematica

    Euler's method

  6. ThindPad x230 无法U盘启动

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  7. HDU 4608 I-number(模拟)

    I-number Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 The I-number of x is defined t ...

  8. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  9. 为什么我刚发表的文章变成了“待审核”,csdn有没有官方解释啊

    为什么我刚发表的文章变成了"待审核",csdn有没有官方解释啊,什么样的文章才会变为待审核呢? 并且从草稿箱和回收站里也看不到我的文章了,希望我的文章没有删掉. 文章的字是一个个打 ...

  10. Linux curl命令详解

    用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...