Skip to content

Commit cb9ef4f

Browse files
committed
Text.padding specifies a padding value which is added to the line width and height when calculating the Text size. ALlows you to add extra spacing if Phaser is unable to accurately determine the true font dimensions (phaserjs#1561 phaserjs#1518)
1 parent b13acdb commit cb9ef4f

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ We've also removed functions and properties from Pixi classes that Phaser doesn'
129129
* Emitter.flow now works in a slightly different (and more useful!) way. You can now specify a `quantity` and a `total`. The `quantity` controls how many particles are emitted every time the flow frequency is met. The `total` controls how many particles will be emitted in total. You can set `total` to be -1 and it will carry on emitting at the given frequency forever (also fixes #1598 thanks @brianbunch)
130130
* ArraySet.removeAll allows you to remove all members of an ArraySet and optionally call `destroy` on them as well.
131131
* GameObject.input.dragStartPoint now stores the coordinates the object was at when the drag started. This value is populated when the drag starts. It can be used to return an object to its pre-drag position, for example if it was dropped in an invalid place in-game.
132+
* Text.padding specifies a padding value which is added to the line width and height when calculating the Text size. ALlows you to add extra spacing if Phaser is unable to accurately determine the true font dimensions (#1561 #1518)
132133

133134
### Updates
134135

src/gameobjects/Text.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Create a new game object for displaying Text.
99
*
10-
* This uses a local hidden Canvas object and renders the type into it. It then makes a texture from this for renderning to the view.
10+
* This uses a local hidden Canvas object and renders the type into it. It then makes a texture from this for rendering to the view.
1111
* Because of this you can only display fonts that are currently loaded and available to the browser: fonts must be pre-loaded.
1212
*
1313
* See {@link http://www.jordanm.co.uk/tinytype this compatibility table} for the available default fonts across mobile browsers.
@@ -57,6 +57,13 @@ Phaser.Text = function (game, x, y, text, style) {
5757
*/
5858
this.type = Phaser.TEXT;
5959

60+
/**
61+
* Specify a padding value which is added to the line width and height when calculating the Text size.
62+
* ALlows you to add extra spacing if Phaser is unable to accurately determine the true font dimensions.
63+
* @property {Phaser.Point} padding
64+
*/
65+
this.padding = new Phaser.Point();
66+
6067
/**
6168
* @property {string} _text - Internal cache var.
6269
* @private
@@ -299,7 +306,7 @@ Phaser.Text.prototype.updateText = function () {
299306

300307
for (var i = 0; i < lines.length; i++)
301308
{
302-
var lineWidth = this.context.measureText(lines[i]).width;
309+
var lineWidth = this.context.measureText(lines[i]).width + this.padding.x;
303310
lineWidths[i] = lineWidth;
304311
maxLineWidth = Math.max(maxLineWidth, lineWidth);
305312
}
@@ -309,7 +316,7 @@ Phaser.Text.prototype.updateText = function () {
309316
this.canvas.width = width * this.resolution;
310317

311318
//calculate text height
312-
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this._lineSpacing;
319+
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this._lineSpacing + this.padding.y;
313320

314321
var height = (lineHeight + this._lineSpacing) * lines.length;
315322

0 commit comments

Comments
 (0)