Class: TextStyle

Phaser.GameObjects. TextStyle

A TextStyle class manages all of the style settings for a Text object.

Text Game Objects create a TextStyle instance automatically, which is accessed via the Text.style property. You do not normally need to instantiate one yourself.


new TextStyle(text, style)

Parameters:
Name Type Description
text Phaser.GameObjects.Text

The Text object that this TextStyle is styling.

style object

The style settings to set.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 64)

Members


align :string

The text alignment.

Type:
  • string
Since: 3.0.0
Default Value:
  • 'left'
Source: src/gameobjects/text/TextStyle.js (Line 223)

backgroundColor :string

The background color.

Type:
  • string
Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 124)

baselineX :number

The amount of horizontal padding adding to the width of the text when calculating the font metrics.

Type:
  • number
Since: 3.3.0
Default Value:
  • 1.2
Source: src/gameobjects/text/TextStyle.js (Line 298)

baselineY :number

The amount of vertical padding adding to the width of the text when calculating the font metrics.

Type:
  • number
Since: 3.3.0
Default Value:
  • 1.4
Source: src/gameobjects/text/TextStyle.js (Line 308)

color :string

The text fill color.

Type:
  • string
Since: 3.0.0
Default Value:
  • '#fff'
Source: src/gameobjects/text/TextStyle.js (Line 133)

fixedHeight :number

The fixed height of the text.

0 means no fixed height.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 255)

fixedWidth :number

The fixed width of the text.

0 means no fixed with.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 243)

fontFamily :string

The font family.

Type:
  • string
Since: 3.0.0
Default Value:
  • 'Courier'
Source: src/gameobjects/text/TextStyle.js (Line 95)

fontSize :string

The font size.

Type:
  • string
Since: 3.0.0
Default Value:
  • '16px'
Source: src/gameobjects/text/TextStyle.js (Line 105)

fontStyle :string

The font style.

Type:
  • string
Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 115)

maxLines :integer

The maximum number of lines to draw.

Type:
  • integer
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 233)

parent :Phaser.GameObjects.Text

The Text object that this TextStyle is styling.

Type:
Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 86)

resolution :number

The resolution the text is rendered to its internal canvas at. The default is 0, which means it will use the resolution set in the Game Config.

Type:
  • number
Since: 3.12.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 267)

rtl :boolean

Whether the text should render right to left.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/gameobjects/text/TextStyle.js (Line 278)

shadowBlur :number

The shadow blur radius.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 193)

shadowColor :string

The shadow color.

Type:
  • string
Since: 3.0.0
Default Value:
  • '#000'
Source: src/gameobjects/text/TextStyle.js (Line 183)

shadowFill :boolean

Whether shadow fill is enabled or not.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/gameobjects/text/TextStyle.js (Line 213)

shadowOffsetX :number

The horizontal shadow offset.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 163)

shadowOffsetY :number

The vertical shadow offset.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 173)

shadowStroke :boolean

Whether shadow stroke is enabled or not.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/gameobjects/text/TextStyle.js (Line 203)

stroke :string

The text stroke color.

Type:
  • string
Since: 3.0.0
Default Value:
  • '#fff'
Source: src/gameobjects/text/TextStyle.js (Line 143)

strokeThickness :number

The text stroke thickness.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/gameobjects/text/TextStyle.js (Line 153)

testString :string

The test string to use when measuring the font.

Type:
  • string
Since: 3.0.0
Default Value:
  • '|MÉqgy'
Source: src/gameobjects/text/TextStyle.js (Line 288)

Methods


destroy()

Destroy this Text Style.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 1053)

getTextMetrics()

Get the current text metrics.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 1012)
Returns:

The text metrics.

Type
BitmapTextMetrics

setAlign(align)

Set the text alignment.

Expects values like 'left', 'right', 'center' or 'justified'.

Parameters:
Name Type Description
align string

The text alignment.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 972)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setBackgroundColor(color)

Set the background color.

Parameters:
Name Type Description
color string

The background color.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 683)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setColor(color)

Set the text fill color.

Parameters:
Name Type Description
color string

The text fill color.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 717)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFill(color)

Set the text fill color.

Parameters:
Name Type Description
color string

The text fill color.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 700)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFixedSize(width, height)

Set a fixed width and height for the text.

Pass in 0 for either of these parameters to disable fixed width or height respectively.

Parameters:
Name Type Description
width number

The fixed width to set.

height number

The fixed height to set.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 652)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFont(font [, updateText])

Set the font.

If a string is given, the font family is set.

If an object is given, the fontFamily, fontSize and fontStyle properties of that object are set.

Parameters:
Name Type Argument Default Description
font string | object

The font family or font settings to set.

updateText boolean <optional>
true

Whether to update the text immediately.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 508)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFontFamily(family)

Set the font family.

Parameters:
Name Type Description
family string

The font family.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 564)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFontSize(size)

Set the font size.

Parameters:
Name Type Description
size number | string

The font size.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 608)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setFontStyle(style)

Set the font style.

Parameters:
Name Type Description
style string

The font style.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 586)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setMaxLines( [max])

Set the maximum number of lines to draw.

Parameters:
Name Type Argument Default Description
max integer <optional>
0

The maximum number of lines to draw.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 993)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setResolution(value)

Set the resolution used by the Text object.

By default it will be set to match the resolution set in the Game Config, but you can override it via this method. It allows for much clearer text on High DPI devices, at the cost of memory because it uses larger internal Canvas textures for the Text.

Please use with caution, as the more high res Text you have, the more memory it uses up.

Parameters:
Name Type Description
value number

The resolution for this Text object to use.

Since: 3.12.0
Source: src/gameobjects/text/TextStyle.js (Line 734)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadow( [x] [, y] [, color] [, blur] [, shadowStroke] [, shadowFill])

Set the shadow settings.

Calling this method always re-measures the parent Text object, so only call it when you actually change the shadow settings.

Parameters:
Name Type Argument Default Description
x number <optional>
0

The horizontal shadow offset.

y number <optional>
0

The vertical shadow offset.

color string <optional>
'#000'

The shadow color.

blur number <optional>
0

The shadow blur radius.

shadowStroke boolean <optional>
false

Whether to stroke the shadow.

shadowFill boolean <optional>
true

Whether to fill the shadow.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 790)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadowBlur( [blur])

Set the shadow blur radius.

Parameters:
Name Type Argument Default Description
blur number <optional>
0

The shadow blur radius.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 868)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadowColor( [color])

Set the shadow color.

Parameters:
Name Type Argument Default Description
color string <optional>
'#000'

The shadow color.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 849)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadowFill(enabled)

Enable or disable shadow fill.

Parameters:
Name Type Description
enabled boolean

Whether shadow fill is enabled or not.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 904)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadowOffset( [x] [, y])

Set the shadow offset.

Parameters:
Name Type Argument Default Description
x number <optional>
0

The horizontal shadow offset.

y number <optional>
0

The vertical shadow offset.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 827)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setShadowStroke(enabled)

Enable or disable shadow stroke.

Parameters:
Name Type Description
enabled boolean

Whether shadow stroke is enabled or not.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 887)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setStroke(color, thickness)

Set the stroke settings.

Parameters:
Name Type Description
color string

The stroke color.

thickness number

The stroke thickness.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 757)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setStyle(style [, updateText] [, setDefaults])

Set the text style.

Parameters:
Name Type Argument Default Description
style object

The style settings to set.

updateText boolean <optional>
true

Whether to update the text immediately.

setDefaults boolean <optional>
false

Use the default values is not set, or the local values.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 349)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text
Example
text.setStyle({
    fontSize: '64px',
    fontFamily: 'Arial',
    color: '#ffffff',
    align: 'center',
    backgroundColor: '#ff00ff'
});

setTestString(string)

Set the test string to use when measuring the font.

Parameters:
Name Type Description
string string

The test string to use when measuring the font.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 635)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setWordWrapCallback(callback [, scope])

Set a custom callback for wrapping lines.

Pass in null to remove wrapping by callback.

Parameters:
Name Type Argument Default Description
callback TextStyleWordWrapCallback

A custom function that will be responsible for wrapping the text. It will receive two arguments: text (the string to wrap), textObject (this Text instance). It should return the wrapped lines either as an array of lines or as a string with newline characters in place to indicate where breaks should happen.

scope object <optional>
null

The scope that will be applied when the callback is invoked.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 946)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

setWordWrapWidth(width [, useAdvancedWrap])

Set the width (in pixels) to use for wrapping lines.

Pass in null to remove wrapping by width.

Parameters:
Name Type Argument Default Description
width number

The maximum width of a line in pixels. Set to null to remove wrapping.

useAdvancedWrap boolean <optional>
false

Whether or not to use the advanced wrapping algorithm. If true, spaces are collapsed and whitespace is trimmed from lines. If false, spaces and whitespace are left as is.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 921)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text

syncFont(canvas, context)

Synchronize the font settings to the given Canvas Rendering Context.

Parameters:
Name Type Description
canvas HTMLCanvasElement

The Canvas Element.

context CanvasRenderingContext2D

The Canvas Rendering Context.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 424)

syncShadow(context, enabled)

Synchronize the shadow settings to the given Canvas Rendering Context.

Parameters:
Name Type Description
context CanvasRenderingContext2D

The Canvas Rendering Context.

enabled boolean

Whether shadows are enabled or not.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 459)

syncStyle(canvas, context)

Synchronize the text style settings to the given Canvas Rendering Context.

Parameters:
Name Type Description
canvas HTMLCanvasElement

The Canvas Element.

context CanvasRenderingContext2D

The Canvas Rendering Context.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 438)

toJSON()

Build a JSON representation of this Text Style.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 1031)
Returns:

A JSON representation of this Text Style.

Type
object

update(recalculateMetrics)

Update the style settings for the parent Text object.

Parameters:
Name Type Description
recalculateMetrics boolean

Whether to recalculate font and text metrics.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 486)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text