Game Development Reference
In-Depth Information
CCLabelTTF is the simplest choice when it comes to displaying text on the screen.
Here's how to create a CCLabelTTF object to display some text:
CCLabelTTF* label = [CCLabelTTF labelWithString:@"text"
[self addChild:label];
In case you're wondering which TrueType fonts are available on iOS devices, you'll
find a list of fonts in the Essentials project for this chapter.
Internally, the given TrueType font is used to render the text on a CCTexture2D tex-
ture. Because this happens every time the text changes, it's not something you should
do every frame. Re-creating the texture of a CCLabelTTF is really slow, and it's done
every time the string of the label is modified:
[label setString:@"new text"];
Also notice that increasing or decreasing the length of the text of a label makes the text
behave as if it's center-aligned on the label's position. The following sentences are
center-aligned to illustrate this effect:
Hello World!
Hello World Once Again!
Hello Our World and all the other Worlds out there!
The center alignment is because of the anchor point and its default position of 0.5,0.5,
which causes the center of the texture (in this case the label's text is the texture) to be
center-aligned with the label's position. In many cases you want to align labels left,
right, up, or down, and you can use the anchorPoint property to easily do that. The
following code shows how you can align a label by simply changing the an-
chorPoint property:
// align label to the right
label.anchorPoint = CGPointMake(1, 0.5f);
// align label to the left
label.anchorPoint = CGPointMake(0, 0.5f);
// align label to the top
label.anchorPoint = CGPointMake(0.5f, 1);
Search Nedrilad ::

Custom Search