Labels and Fonts

本文详细介绍了Cocos2d中两种类型的字体标签:基于TrueType Fonts (TTF)的标签和基于纹理集的标签。包括它们各自的优缺点、创建方式及更新方法等关键信息。

来自:http://blog.sina.com.cn/s/blog_4b194f4f0100iibs.html

 

Introduction

cocos2d supports both TTF (True Type Fonts) labels, and texture atlas labels.

Pros and Cons of TTF labels: ( CCLabel )

  • + All the pros of TTF fonts: any size, kerning support, etc.
  • + Easy to use. No need to use an external editor.
  • - The creation/update is very slow since a new texture will be created

Pros and Cons of texture atlas labels: ( CCLabelAtlasCCBitmapFontAtlas )

  • + The creation / update is very fast, since they don't create a new texture.
  • + Fonts can be customized (shadows, gradients, blur, etc)
  • - Depends on external editors: AngelCode / Hiero editor, GIMP / Photoshop

Label objects

Creating labels: Simple way

The easiest way to create a label is by using the CCLabel object. Example:

CCLabel *label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt"fontSize:24];
 [self add: label]; //maybe addchild

fontName is the TTF font name to be used.

You can use your own custom TTF file. You just need to add the .ttf file to the project. Example of custom TTF file:

CCLabel *label = [CCLabel labelWithString:@"Hello World" fontName:@"Schwarzwald Regular" fontSize:24]; 
[self add: label];
  • cocos2d will try to load the font trying to use the  FontLabel library.
  • If it fails it will use the  UIFont class

Important: The size of the OpenGL texture will be automatically calculated based on the font size and font name.

Creating labels: Complex way

You can also create textures using this API:

CCLabel *left = [CCLabel labelWithString:@"Hello World" dimensions:CGSizeMake(480,50)alignment:UITextAlignmentLeft fontName:@"Marker Felt" fontSize:32]; 
[self add: left];

If you use this way, you should pass the dimension of OpenGL texture to be used. If the texture is not big enough, then only some parts of the label will be rendered.

Possible alignments:

  • UITextAlignmentLeft (left alignment)
  • UITextAlignmentCenter (center alignment)
  • UITextAlignmentRight (right alignment)

Updating

Like any object that implements the CCLabelProtocol protocol you can update it using thesetString method. Example:

[label setString: @"Hello World 2"];

Important: Every time you call setString a NEW OpenGL texture will be created. This means thatsetString is as slow as creating a new CCLabel. So, DO NOT use CCLabel objects if you need to update them frequently. Instead use CCLabelAtlas or CCBitmapFontAtlas.

Alignment

If you want to modify the alignment you can use the anchorPoint property. Example:

//left alignment 
[label setAnchorPoint: ccp(0, 0.5f)];  
 // right alignment 
[label setAnchorPoint: ccp(1, 0.5f)];   
// center aligment (default) 
[label setAnchorPoint: ccp(0.5f, 0.5f)];

Texture Atlas labels

There are 2 types of labels based on texture atlas:

  • CCBitmapFontAtlas
  • CCLabelAtlas

BitmapFontAtlas

Introduction

The CCBitmapFontAtlas is the suggested way to create fast labels since:

  • The bitmap (image) can be customized with the editors
  • You can update/init the label without penalty
  • It is very flexible. Each letter of the label can be treated like an  CCSprite
  • It has kerning support

The CCBitmapFontAtlas label parses the Angel Code Font format to create a label. To create these kind of labels, you can use any of these editors:

Java editors vs. Windows editor:

  • The Windows editor is the official Angel Code editor
  • Java editors: run on Mac
  • Java editors: have additional features like shadow, gradient, blur
Creating a BitmapFontAtlas

To create a CCBitmapFontAtlas object you need to do:

CCBitmapFontAtlas *label = [CCBitmapFontAtlas bitmapFontAtlasWithString:@"Hello World"fntFile:@"bitmapFontTest.fnt"];  // also should add font .png file into the project
[self add:label]
Manipulating each character

Since CCBitmapFontAtlas is a subclass of CCSpriteSheet you can manipulate each character as anCCSprite. The 1st character will be added with tag = 0, the 2nd character will be added with tag=1, and so on. Example:

CCBitmapFontAtlas *label = [CCBitmapFontAtlas bitmapFontAtlasWithString:@"Bitmap Font Atlas" fntFile:@"bitmapFontTest.fnt"];
 CCSprite *char_B = (CCSprite*) [label getChildByTag:0]; // character 'B' 
CCSprite *char_m = (CCSprite*) [label getChildByTag:3]; // character 'm'

(from cocos2d wiki document)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值