mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 15:12:18 +00:00
Merge pull request #2896 from Autophagy/setCharacterLimit
Added feature: set character limit with suffix
This commit is contained in:
commit
f1a3e0b9f6
1 changed files with 32 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue