diff --git a/v2-community/src/gameobjects/Text.js b/v2-community/src/gameobjects/Text.js index 2083e9805..177027a93 100755 --- a/v2-community/src/gameobjects/Text.js +++ b/v2-community/src/gameobjects/Text.js @@ -139,6 +139,18 @@ Phaser.Text = function (game, x, y, text, style) { */ this.splitRegExp = /(?:\r\n|\r|\n)/; + + /** The maximum number of characters that can be set. + * @property {number} characterLimitSize + */ + this.characterLimitSize = -1; + + /** The suffix that is applied to truncated text that is longer than the + * characterLimitSize. + * @property {string} characterLimitSuffix + */ + this.characterLimitSuffix = ''; + /** * @property {number} _res - Internal canvas resolution var. * @private @@ -375,6 +387,10 @@ Phaser.Text.prototype.updateText = function () { var outputText = this.text; + if (this.characterLimitSize > -1 && this.characterLimitSize < outputText.length) { + outputText = this.text.substring(0, this.characterLimitSize) + this.characterLimitSuffix; + } + if (this.style.wordWrap) { outputText = this.runWordWrap(this.text); @@ -1656,6 +1672,22 @@ Phaser.Text.prototype.getBounds = function (matrix) { }; +/** +* Sets the character limit of the text, with a suffix. +* If the text is longer than this limit, it is truncated and the suffix is appended. +* +* @method Phaser.Text#setCharacterLimit +* @param {number} [characterLimit] - The x coordinate of the Text Bounds region. +* @param {string} [suffix] - The suffix to append to the truncated text. +*/ +Phaser.Text.prototype.setCharacterLimit = function (characterLimit, suffix) { + + this.characterLimitSuffix = suffix == undefined ? '' : suffix; + this.characterLimitSize = characterLimit; + + this.updateText(); +} + /** * The text to be displayed by this Text object. * Use a \n to insert a carriage return and split the text.