您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-iphone 文本渲染系统

2013-07-22 22:05 351 查看
Cocos2d中的文本渲染系统(包括两个重要的类:CCLabelTTF和CCLabelBMFont,还有一个不太常用的CCLabelAtlas)



一、CCLabelTTF:

用于显示一些静态的标签和文本,该类继承自CCSprite,使用CCLabelTTF类可以用很少的代码将文本嵌入到游戏之中。

虽然用CCLabelTTF 显示静态标签文本比较方便,但是其渲染速度相当较慢,所以通常用于显示纯文本。

注意在CCLabelTTF类中使用的字体必须是iOS中用到的字体。

可以使用下面的代码在控制台输出所以可以用的字体:

NSMutableArray *fontsNameArray = [[NSMutableArray alloc] init];

NSArray *fontFamilyArray = [UIFont familyNames];

for (NSString *familyName in fontFamilyArray) {

NSLog(@"font family name = %@",familyName);

NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];

for (NSString *fontName in fontNames) {
NSLog(@"font name = %@",fontName);
[fontsNameArray addObject:fontName];
}
}
NSLog(@"font number = %d",[fontsNameArray count]);


关于CCLabelTTF类的使用都比较简单,在其头文件中就可以查看到相关的属性和方法。

例如:

// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];


CCLabelTTF类中有一个方法:

/** changes the string to render
* @warning Changing the string is as expensive as creating a new CCLabelTTF. To obtain better performance use CCLabelAtlas or CCLabelBMFont.
*/
- (void) setString:(NSString*)str;


每次调用这个方法时都会创建一个新的纹理图,因此,如果程序中每一帧都要修改文本内容,使用该方法将会对游戏的性能产生很大的影响。我们可以使用CCLabelBMFont类来提高游戏的性能

二、CCLabelAtlas:CCAtlas的子类

它和CCLabelTTF的主要区别是:

CCLabelAtlas的渲染速度远快于CCLabelTTF;

CCLabelAtlas的字符宽度和高度都是固定的;

CCLabelAtlas中的字符可以是任何形式的,因为他们都来自图片文件

 

CCLabelAtlas的使用相对复杂,在实际开发中通常用CCLabelBMFont代替

CCLabelAtlas这个类比较简单,只定义了两个属性和两三个实例化方法(类方法和实例方法)。

@interface CCLabelAtlas : CCAtlasNode  <CCLabelProtocol>
{
// string to render
NSString		*_string;

// the first char in the charmap
NSUInteger		_mapStartChar;
}

/** creates the CCLabelAtlas with a string, a char map file(the atlas), the width and height of each element in points and the starting char of the atlas */
+(id) labelWithString:(NSString*) string charMapFile: (NSString*) charmapfile itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(NSUInteger)firstElement;

/** creates the CCLabelAtlas with a string and a configuration file
@since v2.0
*/
+(id) labelWithString:(NSString*) string fntFile:(NSString*)fontFile;

/** initializes the CCLabelAtlas with a string, a char map file(the atlas), the width and height in points of each element and the starting char of the atlas */
-(id) initWithString:(NSString*) string charMapFile: (NSString*) charmapfile itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(NSUInteger)firstElement;

/** initializes the CCLabelAtlas with a string, a texture, the width and height in points of each element and the starting char of the atlas */
-(id) initWithString:(NSString*) theString texture:(CCTexture2D*)texture itemWidth:(NSUInteger)w itemHeight:(NSUInteger)h startCharMap:(NSUInteger)c;

/** initializes the CCLabelAtlas with a string and a configuration file
@since v2.0
*/
-(id) initWithString:(NSString*) string fntFile:(NSString*)fontFile;

@end


值得说明的是,这里包含一个属性mapStartChar,初始化方法中也有这个参数,通常情况下是其实际的开始值是字符的ASCII码值。根据所提供的字符集图片,系统会按照所提供的这个ASCII值去截取所需要的字符出来。

如果我们只是需要使用标准的iOS字体,并且无需频繁的修改标签的文本内容,那么使用标准的CCLabelTTF足矣。但是有时候在项目中需要使用定制的字体,而且需要没帧都更改文本中的内容,此时就需要用到CCLabelBMFont

三、CCLabelBMFont

直接继承自CCSpriteBatchNode所以它将文本中的每个字符作为一个独立的CCSprite精灵对象看待。每个字符的旋转角度、大小、着色和透明度等属性都可以修改。

如果不清楚在项目中要使用哪种标签,那么CCLabelBMFont无疑是首选。

所以说它也是显示分数等游戏中动态文本的最佳实践

CCLabelBMFont类中主要的方法如下(大部分是初始化创建一个label的方法):

/** creates a BMFont label with an initial string and the FNT file. */
+(id) labelWithString:(NSString*)string fntFile:(NSString*)fntFile;
/** creates a BMFont label with an initial string, the FNT file, width, and alignment option */
+(id) labelWithString:(NSString*)string fntFile:(NSString*)fntFile width:(float)width alignment:(CCTextAlignment)alignment;
/** creates a BMFont label with an initial string, the FNT file, width, alignment option and the offset of where the glyphs start on the .PNG image */
+(id) labelWithString:(NSString*)string fntFile:(NSString*)fntFile width:(float)width alignment:(CCTextAlignment)alignment imageOffset:(CGPoint)offset;

/** init a BMFont label with an initial string and the FNT file */
-(id) initWithString:(NSString*)string fntFile:(NSString*)fntFile;
/** init a BMFont label with an initial string and the FNT file, width, and alignment option*/
-(id) initWithString:(NSString*)string fntFile:(NSString*)fntFile width:(float)width alignment:(CCTextAlignment)alignment;
/** init a BMFont label with an initial string and the FNT file, width, alignment option and the offset of where the glyphs start on the .PNG image */
-(id) initWithString:(NSString*)string fntFile:(NSString*)fntFile width:(float)width alignment:(CCTextAlignment)alignment imageOffset:(CGPoint)offset;


可以看到里面有一个重要的参数:fntFile  (字体文件,其实就是一个字符图集(一张图片),其中包含了所以要显示的字符,及其描述字符在字符图集中的坐标数据。)
它的作用就相当于精灵表单,而其中的每一个字符则相当于精灵表单中的单个精灵对象。

可以使用一些第三方的工具创建字体纹理图集,也就是字体文件啦。我一直是使用Glyph Designer(虽然说是收费的,但是也是有破解版的,你懂的!

 下载地址 )

简单说一下如何使用Glyph Designer,选择处理好你中意的字体,然后在File中选择Export就ok啦,生成以png图片文件和一个fnt文件。使用的时候,导入xcode项目中就可以了。

四、标签对齐方式

可以通过设置不同的锚点值来设置标签的不同对齐方式。

//尝试设置不同的锚点值
CCLabelTTF *label4 = [CCLabelTTF labelWithString:@"对齐方式" fontName:@"Cochin" fontSize:20];

//右对齐
// label4.anchorPoint = ccp(0,0.5f);

//左对齐
// label4.anchorPoint = ccp(1,0.5f);

//顶部对齐
// label4.anchorPoint = ccp(0.5f,0);

//底部对齐
// label4.anchorPoint = ccp(0.5f,1.0f);

//默认的几何中心位置
label4.anchorPoint = ccp(0.5f,0.5f);

如果修改锚点的位置,那么在旋转节点对象的时候也会以锚点位置为基础。

大致就是这些了,很多参看了cocos2d权威指南一书。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息