Class

SKLabelNode

A node that displays a text label.

Overview

Just about every game needs to display text at some point, even if it is just to display “Game Over” to the player. If you had to implement this yourself in OpenGL, it would take a fair amount of work to get it correct. But the SKLabelNode class does all of the work necessary to load fonts and create text for display.

To use a label node, create a new label node by calling the labelNodeWithFontNamed: class method. Then configure the other label properties, especially the text property. The size of a label node is determined implicitly by the label node’s fontName, fontSize, and text properties.

By default, the text label is centered horizontally on the label node’s origin, with the font’s baseline passing through the origin. The verticalAlignmentMode and horizontalAlignmentMode properties can be used to adjust the label’s position relative to the origin.

SKLabelNode demonstrates how to create a new text label.

Listing 1

Adding a text label

        SKLabelNode *winner = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        winner.text = @"You Win!";
        winner.fontSize = 65;
        winner.fontColor = [SKColor greenColor];
        winner.position = CGPointMake(CGRectGetMidX(self.bounds),
                                     CGRectGetMidY(self.bounds));
        [self addChild:winner];

Whenever you change the label node’s properties, the label node is automatically updated the next time the scene is rendered.

Symbols

Creating a New Label Node

init(fontNamed: String?)

Initializes a new label object with a specified font.

init(text: String?)

Initializes a new label object with a text string.

Configuring the Label Message

var text: String?

The string that the label node displays.

Configuring the Label Font

var fontColor: UIColor?

The color of the label.

var fontName: String?

The font used for the text in the label.

var fontSize: CGFloat

The size of the font used in the label.

Configuring the Label’s Position

var verticalAlignmentMode: SKLabelVerticalAlignmentMode

The vertical position of the text within the node.

var horizontalAlignmentMode: SKLabelHorizontalAlignmentMode

The horizontal position of the text within the node.

Performing Color Blending

var color: UIColor?

The label’s blend color.

var colorBlendFactor: CGFloat

A floating-point value that describes how the color is blended with the font color.

Blending the Label into the Framebuffer

var blendMode: SKBlendMode

The blend mode used to draw the label into the parent’s framebuffer.

Constants

SKLabelVerticalAlignmentMode

Options for aligning text vertically.

SKLabelHorizontalAlignmentMode

Options for aligning text horizontally.

Relationships

Inherits From

Conforms To