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.Listing 1 demonstrates how to create a new text label.

Listing 1

Adding a text label

let winner = SKLabelNode(fontNamed: "Chalkduster")
winner.text = "You Win!"
winner.fontSize = 65
winner.fontColor = SKColor.green
winner.position = CGPoint(x: frame.midX, y: frame.midY)
   
addChild(winner)

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

Topics

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

enum SKLabelVerticalAlignmentMode

Options for aligning text vertically.

enum SKLabelHorizontalAlignmentMode

Options for aligning text horizontally.

Relationships

Inherits From

See Also

Nodes That Draw Content

class SKSpriteNode

A node that draws a rectangular texture, image or color.

class SKShapeNode

A node that renders a shape defined by a Core Graphics path.

class SKVideoNode

A node that displays video content.

class SKCropNode

A node that masks pixels drawn by its children so that only some are rendered to the parent’s frame buffer.

class SKReferenceNode

A node that creates its children from an archived collection of other nodes.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software