Class: TextStyle

Phaser.GameObjects.Text. TextStyle

Style settings for a Text object.


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 219)

backgroundColor :string

The background color.

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

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 294)

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 304)

color :string

The text fill color.

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

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 251)

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 239)

fontFamily :string

The font family.

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

fontSize :string

The font size.

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

fontStyle :string

The font style.

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

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 229)

parent :Phaser.GameObjects.Text

The Text object that this TextStyle is styling.

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

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 263)

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 274)

shadowBlur :number

The shadow blur radius.

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

shadowColor :string

The shadow color.

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

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 209)

shadowOffsetX :number

The horizontal shadow offset.

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

shadowOffsetY :number

The vertical shadow offset.

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

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 199)

stroke :string

The text stroke color.

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

strokeThickness :number

The text stroke thickness.

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

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 284)

Methods


destroy()

Destroy this Text Style.

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

getTextMetrics()

Get the current text metrics.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 1008)
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 968)
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 679)
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 713)
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 696)
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 648)
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 504)
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 560)
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 604)
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 582)
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 989)
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 730)
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 786)
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 864)
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 845)
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 900)
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 823)
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 883)
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 753)
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 345)
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 631)
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 942)
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 917)
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 420)

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 455)

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 434)

toJSON()

Build a JSON representation of this Text Style.

Since: 3.0.0
Source: src/gameobjects/text/TextStyle.js (Line 1027)
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 482)
Returns:

The parent Text object.

Type
Phaser.GameObjects.Text