setTextBounds(x, y, width, height) → {Phaser.Text}
The Text Bounds is a rectangular region that you control the dimensions of into which the Text object itself is positioned,
regardless of the number of lines in the text, the font size or any other attribute.
Alignment is controlled via the properties boundsAlignH
and boundsAlignV
within the Text.style object, or can be directly
set through the setters Text.boundsAlignH
and Text.boundsAlignV
. Bounds alignment is independent of text alignment.
For example: If your game is 800x600 in size and you set the text bounds to be 0,0,800,600 then by setting boundsAlignH to
'center' and boundsAlignV to 'bottom' the text will render in the center and at the bottom of your game window, regardless of
how many lines of text there may be. Even if you adjust the text content or change the style it will remain at the bottom center
of the text bounds.
This is especially powerful when you need to align text against specific coordinates in your game, but the actual text dimensions
may vary based on font (say for multi-lingual games).
If Text.wordWrapWidth
is greater than the width of the text bounds it is clamped to match the bounds width.
Call this method with no arguments given to reset an existing textBounds.
It works by calculating the final position based on the Text.canvas size, which is modified as the text is updated. Some fonts
have additional padding around them which you can mitigate by tweaking the Text.padding property. It then adjusts the pivot
property based on the given bounds and canvas size. This means if you need to set the pivot property directly in your game then
you either cannot use setTextBounds
or you must place the Text object inside another DisplayObject on which you set the pivot.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
x | number | <optional> | The x coordinate of the Text Bounds region. |
y | number | <optional> | The y coordinate of the Text Bounds region. |
width | number | <optional> | The width of the Text Bounds region. |
height | number | <optional> | The height of the Text Bounds region. |
Returns
This Text instance.
- Source code: gameobjects/Text.js (Line 1347)
Please login to continue.