Text Rendering System in cocos2d (including two important classes: cclabelttf and cclabelbmfont, and a less commonly used cclabelatlas)
I. cclabelttf:
This class is used to display static labels and text that inherit from ccsprite. The cclabelttf class can be used to embed text into the game with very little code.
Although it is convenient to use cclabelttf to display static label text, it is relatively slow to render, so it is usually used to display plain text.
Note that the fonts used in the cclabelttf class must be those used in IOS.
You can use the following code to output the font in the console:
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]);
The use of the cclabelttf class is relatively simple. You can view the relevant attributes and methods in the header file.
For example:
// create and initialize a LabelCCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
The cclabelttf class has a method:
/** 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;
Each time this method is called, a new texture image is created. Therefore, if you want to modify the text content for each frame in the program, this method will have a great impact on the performance of the game. We can use the cclabelbmfont class to improve the game performance.
Ii. cclabelatlas: a subclass of ccatlas
The main difference between it and cclabelttf is:
The rendering speed of cclabelatlas is much faster than that of cclabelttf;
The character width and height of cclabelatlas are fixed;
Characters in cclabelatlas can be in any form because they are from image files.
The use of cclabelatlas is relatively complex. In actual development, it is usually replaced by cclabelbmfont.
The class cclabelatlas is relatively simple. It only defines two attributes and two or three instantiation methods (class methods and instance methods ).
@interface CCLabelAtlas : CCAtlasNode <CCLabelProtocol>{// string to renderNSString*_string;// the first char in the charmapNSUInteger_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
It is worth noting that this includes a property mapstartchar, which is also included in the initialization method. Generally, its actual start value is the ASCII value of the character. Based on the provided character set image, the system captures the required characters according to the provided ASCII value.
If we only need to use the standard IOS font and do not need to modify the text content of tags frequently, we can use the standard cclabelttf. But sometimes you need to use a Custom font in the project and change the content in the text without frame. In this case, you need to use cclabelbmfont.
III,Cclabelbmfont:
It directly inherits from ccspritebatchnode, so it treats each character in the text as an independent ccsprite sprite object. The rotation angle, size, coloring, transparency, and other attributes of each character can be modified.
If you do not know which tag you want to use in the project, cclabelbmfont is the first choice.
Therefore, it is also the best practice for displaying dynamic texts in games such as scores.
The main methods in the cclabelbmfont class are as follows (most of them are the methods for initializing and creating a 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;
It can be seen that there is an important parameter: fntfile (font file, in fact, is a character Gallery (an image), which contains the characters to be displayed, coordinate data of the descriptive characters in the character gallery .)
It is equivalent to a Sprite form, and each character in it is equivalent to a single sprite object in the sprite form.
You can use some third-party tools to create a font texture Gallery, that is, a font file. I have been using glyph designer (although it is charged, it also has a cracked version, you know! )
Let's briefly describe how to use glyph designer, select and process your favorite font, select export in file, and generate a PNG image file and a fnt file. When using it, you can import it to the xcode project.
Iv. Tag alignment
You can set different Alignment Methods for tags by setting different anchor values.
// Try to set different anchpoint values: cclabelttf * label4 = [cclabelttf labelwithstring: @ "alignment mode" fontname: @ "login in" fontsize: 20]; // right-aligned // label4.anchorpoint = CCP (0, 0.5f); // left-aligned // label4.anchorpoint = CCP (1, 0.5f ); // top alignment // label4.anchorpoint = CCP (0.5f, 0); // bottom alignment // label4.anchorpoint = CCP (0.5f, 1.0f ); // label4.anchorpoint = CCP (0.5f, 0.5f );
If you modify the position of the anchor, the anchor object is also rotated Based on the anchor position.
This is basically the case. For more information, see the cocos2d authoritative guide.