cocos2d-x 字体使用方法浅析

cocos2d 使用字体的类大概分为三种:
CCLabelAtlas、CCLabelBMFont、CCLabelTTF。
下面分别介绍这三种字体类提供的接口及一般初始化方法。
一、首先是我用得比较熟悉的CCLableTTF
1、char * description();
该函数会得到当前label使用的字体名称及当前字体大小信息。
2、static CCLabelTTF * labelWithString(const char *label, const char *fontName, float fontSize);
这是一个静态初始化函数,该方法返回一个CCLabelTTF对象指针。
参数含义: label:需要显示的字符串,fontName:使用字体名称,fontSize:设置字体大小
附上cocos2d字体列表:http://blog.sina.com.cn/s/blog_82ca0a7701011271.html
3、bool initWithString(const char *label, const char *fontName, float fontSize);
初始化label,参数含义与上一个函数相同。这一函数与上一函数的区别为:本函数不是静态函数,也就是说你必须实例化一个CCLabelTTF对象之后才能调用本函数。
4、virtual void setString(const char label);
可以看到这是个虚函数,继承自CCLabelProtocol类。本函数的作用就是改变当前label显示的字符串。
5、virtual const char
getString(void);
这也是一个继承自CCLabelProtocol的虚函数,功能为得到当前label显示的字符串。
二、然后是CCLabelBMFont
1、static void purgeCachedData();
顾名思义,该函数作用为清空缓存数据。
2、static CCLabelBMFont * labelWithString(const char *str, const char *fntFile);
这也是一个静态初始化函数.
需要提供的参数信息为: (const char *)str:要显示的字符串,(const char *)fntFile:字体坐标文件
说明:字体坐标文件,与其对应的是一个同名的png文件,里面保存了字体样本,显示的时候就是通过fnt坐标文件从png里面选择字符并显示,如图


字体使用方法浅析" />


字体使用方法浅析" /> 图1 bitmapFontChinese.fnt 部分内容 图2 bitmapFontChinese.png 字体样本
至于其具体工作原理就不深究了,这里只要知道使用方法就行。
3、bool initWithString(const char *str, const char *fntFile);
参数含义同上。
4、virtual void setString(const char label);
virtual const char
getString(void);
这两个函数同样是继承自CCLabelProtocol的函数。作用就不多说了。
三、最后是CCLabelAtlas,这个比较复杂,所以放在最后讲
需要注意的是,使用这个类提供的方法需要你提供字体图片,而且CCLabelAtlas只支持定尺寸大小的字体显示,也就是说你给出的字体图片中的每个字符必须等尺寸。初始化的时候也必须给出每个字符的宽和高,以便于程序自动取图。
其工作原理个人认为是根据ACSII工作,给出第一个字符,程序自动得到其ASCII码,然后再依次得到需要显示的字符串中每个字符的ASCII码并计算其位置,所以字体图片中的字符必须按ACSII码排列,不然得不到想要的字符串
1、static CCLabelAtlas * labelWithString(const char *label, const char *charMapFile,
unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap);
一个函数都要分两行来写,足以证明其复杂性。
参数含义: (const char *)label:当然是要显示的字符串了,(const char *)charMapFile:上面所说的字体图片
(unsigned int) itemWidth、itemHeight:字符的尺寸,也就是宽和高,这个必须根据你的字体图片来给出值
(unsigned char)startCharMap:从哪个字符开始,一般是字体图片的第一个字符
2、bool initWithString(const char *label, const char *charMapFile,
unsigned int itemWidth, unsigned int itemHeight, unsigned char startCharMap);
各参数含义同上一个函数。
3、virtual void setString(const char label);
virtual const char
getString(void);
这两个函数在三种字体类中都一样,都是继承自CCLabelProtocol。详见CCLabelTTF中的描述
四、总结
1、项目需要,所以看了看字体相关的东西,可能还很浅显,有错的地方希望大家给予指正。
2、如果有更好的见解或建议,欢迎指教或者一起讨论
3、本文只是给出一般接口及参数的介绍,如果想深入了解其继承关系或三种方式的工作效率
http://zhongyi.sina.com/zx/news/315400.shtml http://zhongyi.sina.com/zx/news/315399.shtml http://zhongyi.sina.com/zx/news/315398.shtml http://zhongyi.sina.com/zx/news/315396.shtml http://zhongyi.sina.com/zx/news/315395.shtml http://zhongyi.sina.com/zx/news/315392.shtml http://zhongyi.sina.com/zx/news/315391.shtml http://zhongyi.sina.com/zx/news/315390.shtm http://zhongyi.sina.com/zx/news/315389.shtml http://zhongyi.sina.com/zx/news/315388.shtml http://zhongyi.sina.com/zx/news/315386.shtml http://zhongyi.sina.com/zx/news/315385.shtml http://zhongyi.sina.com/zx/news/315384.shtml http://zhongyi.sina.com/zx/news/315383.shtml http://zhongyi.sina.com/zx/news/315381.shtml http://zhongyi.sina.com/zx/news/315380.shtm http://zhongyi.sina.com/zx/news/315378.shtml http://zhongyi.sina.com/zx/news/315377.shtm http://zhongyi.sina.com/zx/news/315376.shtml http://zhongyi.sina.com/zx/news/315375.shtml http://zhongyi.sina.com/zx/news/315374.shtml http://zhongyi.sina.com/zx/news/315372.shtml http://zhongyi.sina.com/zx/news/315371.shtml http://zhongyi.sina.com/zx/news/315370.shtml http://zhongyi.sina.com/zx/news/315369.shtml http://zhongyi.sina.com/zx/news/315367.shtml http://zhongyi.sina.com/zx/news/315366.shtml http://zhongyi.sina.com/zx/news/315365.shtm http://zhongyi.sina.com/zx/news/315363.shtml http://zhongyi.sina.com/zx/news/315362.shtml http://zhongyi.sina.com/zx/news/316212.shtml http://zhongyi.sina.com/zx/news/316211.shtml http://zhongyi.sina.com/zx/news/316210.shtml http://zhongyi.sina.com/zx/news/316209.shtml http://zhongyi.sina.com/zx/news/316208.shtml http://zhongyi.sina.com/zx/news/316207.shtml http://zhongyi.sina.com/zx/news/316206.shtml http://zhongyi.sina.com/zx/news/316205.shtml http://zhongyi.sina.com/zx/news/316204.shtm http://zhongyi.sina.com/zx/news/316203.shtml http://zhongyi.sina.com/zx/news/316202.shtml http://zhongyi.sina.com/zx/news/316201.shtml http://zhongyi.sina.com/zx/news/316200.shtml http://zhongyi.sina.com/zx/news/316199.shtml http://zhongyi.sina.com/zx/news/316198.shtml http://zhongyi.sina.com/zx/news/316197.shtml http://zhongyi.sina.com/zx/news/316196.shtml http://zhongyi.sina.com/zx/news/316195.shtm http://zhongyi.sina.com/zx/news/316194.shtm http://zhongyi.sina.com/zx/news/316192.shtml http://zhongyi.sina.com/zx/news/316191.shtml http://zhongyi.sina.com/zx/news/316190.shtml http://zhongyi.sina.com/zx/news/316187.shtm http://zhongyi.sina.com/zx/news/316184.shtml http://zhongyi.sina.com/zx/news/316181.shtml http://zhongyi.sina.com/zx/news/316179.shtml http://zhongyi.sina.com/zx/news/316177.shtml http://zhongyi.sina.com/zx/news/316176.shtml http://zhongyi.sina.com/zx/news/316175.shtm http://zhongyi.sina.com/zx/news/316174.shtml http://zhongyi.sina.com/zx/news/317416.shtml http://zhongyi.sina.com/zx/news/317414.shtm http://zhongyi.sina.com/zx/news/317413.shtml http://zhongyi.sina.com/zx/news/317412.shtml http://zhongyi.sina.com/zx/news/317411.shtml http://zhongyi.sina.com/zx/news/317410.shtml http://zhongyi.sina.com/zx/news/317409.shtml http://zhongyi.sina.com/zx/news/317406.shtml http://zhongyi.sina.com/zx/news/317405.shtml http://zhongyi.sina.com/zx/news/317403.shtml http://zhongyi.sina.com/zx/news/317402.shtml http://zhongyi.sina.com/zx/news/317401.shtml http://zhongyi.sina.com/zx/news/317399.shtml http://zhongyi.sina.com/zx/news/317397.shtml http://zhongyi.sina.com/zx/news/317395.shtml http://zhongyi.sina.com/zx/news/317393.shtml http://zhongyi.sina.com/zx/news/317392.shtml http://zhongyi.sina.com/zx/news/317390.shtml http://zhongyi.sina.com/zx/news/317389.shtml http://zhongyi.sina.com/zx/news/317388.shtml http://zhongyi.sina.com/zx/news/317387.shtm http://zhongyi.sina.com/zx/news/317386.shtml http://zhongyi.sina.com/zx/news/317385.shtm http://zhongyi.sina.com/zx/news/317384.shtml http://zhongyi.sina.com/zx/news/317383.shtml http://zhongyi.sina.com/zx/news/317382.shtml http://zhongyi.sina.com/zx/news/317380.shtml http://zhongyi.sina.com/zx/news/317379.shtml http://zhongyi.sina.com/zx/news/317378.shtml http://zhongyi.sina.com/zx/news/317377.shtml

马克,露珠能不能讲讲cocoStudio中的label动态设置中文的方法