CCLabelTTF Class Reference

Inherits from CCSprite : CCNode : CCResponder : NSObject
Conforms to CCLabelProtocol
Declared in CCLabelTTF.h

Overview

CCLabelTTF displays text rendered using a TrueType (TTF, OTF) font.

Attributed strings are supported on Mac and iOS6+ which allow for greater text formatting control.

Performance Note

Each time the label’s string property changes a new CCLabelTTF texture is created and rendered, and the previous one discarded. This adds a huge performance penalty on every text change. If you need to frequently update a label’s text, you have two options:

  • Use CCLabelBMFont which does not incur such a penalty. Requires creating a font with an external bitmap font editing tool.
  • Create and cache all possible string variants as individual CCLabelTTF. Only suitable if the number of possible string permutations is low and all strings are known beforehand. For example, assuming you use a fixed-width font you could use label nodes with digits 0 through 9 and create your own digit counter node using n times ten label nodes, where n stands for the total number of digits you expect to display at most. However consider the next paragraph.

Memory Usage Note

Each label, even when using the same string and font and all other properties being identical, will still create its own texture.

So if you have 100 label nodes with a “Hello” string you will have 100 “Hello” textures in memory. This behavior is unlike other nodes, for instace sprites created from the same image file load and share identical textures.

If you use many labels in the same scene you should look into bitmap fonts and CCLabelBMFont.

iOS Fonts List

Here’s a list of fonts built into iOS and their font names. Fonts not in this list will have iOS automatically pick the font that closely resembles the desired font, or a default font.

Custom Fonts

It is possible to add custom truetype fonts to an iOS application and use the font with CCLabelTTF. For instructions and troubleshooting carefully read this Q&A post.

Note that a very common mistake is to assume the font filename or family name as the fontName property. Assuming you have a font named “Avenir Condensed” then you need to find out the actual font identifier, which may be labelled AvenirNextCondensed-HeavyItalic. That’s the identifier you need to use. A typical giveaway that you got the wrong identifier is if the string contains a space character.

Creating a Truetype Font Label

+ labelWithString:fontName:fontSize:

Creates and returns a label object using the specified text, font name and font size.

+ (instancetype)labelWithString:(NSString *)string fontName:(NSString *)name fontSize:(CGFloat)size

Parameters

string

Label text.

name

Label font name.

size

Label font size (in points).

Return Value

The CCLabelTTF Object.

Declared In

CCLabelTTF.h

+ labelWithString:fontName:fontSize:dimensions:

Creates and returns a label object using the specified text, font name, font size and dimensions.

+ (instancetype)labelWithString:(NSString *)string fontName:(NSString *)name fontSize:(CGFloat)size dimensions:(CGSize)dimensions

Parameters

string

Label text.

name

Label font name.

size

Label font size (in points).

dimensions

Label dimensions.

Return Value

The CCLabelTTF Object.

Declared In

CCLabelTTF.h

– initWithString:fontName:fontSize:

Initializes and returns a label object using the specified text, font name and font size.

- (id)initWithString:(NSString *)string fontName:(NSString *)name fontSize:(CGFloat)size

Parameters

string

Label text.

name

Label font name.

size

Label font size (in points).

Return Value

An initialized CCLabelTTF Object.

Declared In

CCLabelTTF.h

– initWithString:fontName:fontSize:dimensions:

Initializes and returns a label object using the specified text, font name, font size and dimensions.

- (id)initWithString:(NSString *)string fontName:(NSString *)name fontSize:(CGFloat)size dimensions:(CGSize)dimensions

Parameters

string

Label text.

name

Label font name.

size

Label font size (in points).

dimensions

Label dimensions.

Return Value

An initialized CCLabelTTF Object.

Declared In

CCLabelTTF.h

Creating an Attributed Truetype Font Label

+ labelWithAttributedString:

Creates and returns a label object using the specified attributed text.

+ (instancetype)labelWithAttributedString:(NSAttributedString *)attrString

Parameters

attrString

Label Attributed text.

Return Value

The CCLabelTTF Object.

Availability

Available in iOS 6.0+ and OS X.

Declared In

CCLabelTTF.h

+ labelWithAttributedString:dimensions:

Creates and returns a label object using the specified attributed text and dimensions.

+ (instancetype)labelWithAttributedString:(NSAttributedString *)attrString dimensions:(CGSize)dimensions

Parameters

attrString

Label Attributed text.

dimensions

Label dimensions.

Return Value

The CCLabelTTF Object.

Availability

Available in iOS 6.0+ and OS X.

Declared In

CCLabelTTF.h

– initWithAttributedString:

Initializes and returns a label object using the specified attributed text.

- (id)initWithAttributedString:(NSAttributedString *)attrString

Parameters

attrString

Label Attributed text.

Return Value

An initialized CCLabelTTF Object.

Availability

Available in iOS 6.0+ and OS X.

Declared In

CCLabelTTF.h

– initWithAttributedString:dimensions:

Initializes and returns a label object using the specified attributed text and dimensions.

- (id)initWithAttributedString:(NSAttributedString *)attrString dimensions:(CGSize)dimensions

Parameters

attrString

Label Attributed text.

dimensions

Label dimensions.

Return Value

An initialized CCLabelTTF Object.

Availability

Available in iOS 6.0+ and OS X.

Declared In

CCLabelTTF.h

Text Attributes

  string

The label text.

@property (nonatomic, copy) NSString *string

Declared In

CCLabelTTF.h

  attributedString

The label’s attributed text.

@property (nonatomic, copy) NSAttributedString *attributedString

Declared In

CCLabelTTF.h

  fontName

The platform font name to use for the text.

@property (nonatomic, strong) NSString *fontName

Declared In

CCLabelTTF.h

  fontSize

The font size of the text.

@property (nonatomic, assign) CGFloat fontSize

Declared In

CCLabelTTF.h

  fontColor

The color of the text. Does not apply if you are using shadow or outline effects.

@property (nonatomic, strong) CCColor *fontColor

See Also

Declared In

CCLabelTTF.h

  horizontalAlignment

The horizontal alignment of the text.

@property (nonatomic, assign) CCTextAlignment horizontalAlignment

See Also

Declared In

CCLabelTTF.h

  verticalAlignment

The vertical alignment of the text.

@property (nonatomic, assign) CCVerticalTextAlignment verticalAlignment

Declared In

CCLabelTTF.h

Sizing the Label

  dimensions

Dimensions of the label in Points, including padding.

@property (nonatomic, assign) CGSize dimensions

Declared In

CCLabelTTF.h

  dimensionsType

Dimension type of the label.

@property (nonatomic, assign) CCSizeType dimensionsType

See Also

Declared In

CCLabelTTF.h

  adjustsFontSizeToFit

If YES, the label will be scaled down to fit into the size provided by the dimensions property. Only has an effect if dimensions are set.

@property (nonatomic, assign) BOOL adjustsFontSizeToFit

Declared In

CCLabelTTF.h

  minimumFontSize

Used together with adjustsFontSizeToFit. Fonts will not be scaled down below this size (the label will instead be clipped).

@property (nonatomic, assign) CGFloat minimumFontSize

Declared In

CCLabelTTF.h

  baselineAdjustment

Adjusts the font’s baseline, the value is in points.

@property (nonatomic, assign) CGFloat baselineAdjustment

Declared In

CCLabelTTF.h

Drawing a Shadow

  shadowColor

The color of the text shadow. If the color is fully transparent, no shadow will be used.

@property (nonatomic, strong) CCColor *shadowColor

Declared In

CCLabelTTF.h

  shadowOffset

The offset of the shadow.

@property (nonatomic, assign) CGPoint shadowOffset

Declared In

CCLabelTTF.h

  shadowOffsetInPoints

The offset of the shadow in points

@property (nonatomic, readonly) CGPoint shadowOffsetInPoints

Declared In

CCLabelTTF.h

  shadowOffsetType

The position type to be used for the shadow offset.

@property (nonatomic, assign) CCPositionType shadowOffsetType

See Also

Declared In

CCLabelTTF.h

  shadowBlurRadius

The blur radius of the shadow.

@property (nonatomic, assign) CGFloat shadowBlurRadius

Declared In

CCLabelTTF.h

Drawing an Outline

  outlineColor

The color of the text’s outline.

@property (nonatomic, strong) CCColor *outlineColor

See Also

Declared In

CCLabelTTF.h

  outlineWidth

The width of the text’s outline.

@property (nonatomic, assign) CGFloat outlineWidth

Declared In

CCLabelTTF.h

HTML String (OS X only)

– setHTML:

Creates an attributed string from HTML text.

- (void)setHTML:(NSString *)html

Parameters

html

HTML string.

Availability

Only available on OS X.

Declared In

CCLabelTTF.h

Register a Custom Truetype Font

+ registerCustomTTF:

Register a TTF font resource added to the project/bundle.

+ (NSString *)registerCustomTTF:(NSString *)fontFile

Parameters

fontFile

Full or relative path to font file.

Return Value

Registered font name. Returns nil if the font file failed to register.

Declared In

CCLabelTTF.h