Skip to content

Commit 198f8d4

Browse files
committed
Added feature: set character limit with suffix
1 parent 58d09f8 commit 198f8d4

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

  • v2-community/src/gameobjects

v2-community/src/gameobjects/Text.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ Phaser.Text = function (game, x, y, text, style) {
139139
*/
140140
this.splitRegExp = /(?:\r\n|\r|\n)/;
141141

142+
143+
/** The maximum number of characters that can be set.
144+
* @property {number} characterLimitSize
145+
*/
146+
this.characterLimitSize = -1;
147+
148+
/** The suffix that is applied to truncated text that is longer than the
149+
* characterLimitSize.
150+
* @property {string} characterLimitSuffix
151+
*/
152+
this.characterLimitSuffix = '';
153+
142154
/**
143155
* @property {number} _res - Internal canvas resolution var.
144156
* @private
@@ -375,6 +387,10 @@ Phaser.Text.prototype.updateText = function () {
375387

376388
var outputText = this.text;
377389

390+
if (this.characterLimitSize > -1 && this.characterLimitSize < outputText.length) {
391+
outputText = this.text.substring(0, this.characterLimitSize) + this.characterLimitSuffix;
392+
}
393+
378394
if (this.style.wordWrap)
379395
{
380396
outputText = this.runWordWrap(this.text);
@@ -1656,6 +1672,22 @@ Phaser.Text.prototype.getBounds = function (matrix) {
16561672

16571673
};
16581674

1675+
/**
1676+
* Sets the character limit of the text, with a suffix.
1677+
* If the text is longer than this limit, it is truncated and the suffix is appended.
1678+
*
1679+
* @method Phaser.Text#setCharacterLimit
1680+
* @param {number} [characterLimit] - The x coordinate of the Text Bounds region.
1681+
* @param {string} [suffix] - The suffix to append to the truncated text.
1682+
*/
1683+
Phaser.Text.prototype.setCharacterLimit = function (characterLimit, suffix) {
1684+
1685+
this.characterLimitSuffix = suffix == undefined ? '' : suffix;
1686+
this.characterLimitSize = characterLimit;
1687+
1688+
this.updateText();
1689+
}
1690+
16591691
/**
16601692
* The text to be displayed by this Text object.
16611693
* Use a \n to insert a carriage return and split the text.

0 commit comments

Comments
 (0)