BitmapText has a new maxWidth property that will attempt to wrap the text if it exceeds the width specified.

This commit is contained in:
photonstorm 2015-02-18 22:36:12 +00:00
parent ba727555c8
commit 88ab104da4
2 changed files with 30 additions and 2 deletions

View file

@ -151,6 +151,7 @@ We've also removed functions and properties from Pixi classes that Phaser doesn'
* InputHandler.enableDrag with a boundsSprite set now takes into account both the Sprites anchor and the boundsSprite anchor when limiting the drag.
* Sound in Web Audio now uses AudioContext.onended to trigger when it will stop playing instead of using a time based value. This is only used if the sound doesn't loop and isn't an audio sprite, but will give a much more accurate `Sound.onStop` event. It also prevents short audio files from being cut off during playback (#1471) and accounts for time spent decoding.
* If you load an image and provide a key that was already in-use in the Cache, then the old image is now destroyed (via `Cache.removeImage`) and the new image takes its place.
* BitmapText has a new `maxWidth` property that will attempt to wrap the text if it exceeds the width specified.
### Bug Fixes

View file

@ -40,6 +40,15 @@ PIXI.BitmapText = function(text, style)
*/
this.textHeight = 0;
/**
* The max width of this bitmap text in pixels. If the text provided is longer than the value provided, line breaks will be
* automatically inserted in the last whitespace. Disable by setting value to 0.
*
* @property maxWidth
* @type Number
*/
this.maxWidth = 0;
/**
* @property _pool
* @type Array
@ -113,12 +122,14 @@ PIXI.BitmapText.prototype.updateText = function()
var lineWidths = [];
var line = 0;
var scale = this.fontSize / data.size;
var lastSpace = 0;
for(var i = 0; i < this.text.length; i++)
for (var i = 0; i < this.text.length; i++)
{
var charCode = this.text.charCodeAt(i);
lastSpace = /(\s)/.test(this.text.charAt(i)) ? i : lastSpace;
if(/(?:\r\n|\r|\n)/.test(this.text.charAt(i)))
if (/(?:\r\n|\r|\n)/.test(this.text.charAt(i)))
{
lineWidths.push(pos.x);
maxLineWidth = Math.max(maxLineWidth, pos.x);
@ -130,6 +141,22 @@ PIXI.BitmapText.prototype.updateText = function()
continue;
}
if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth)
{
chars.splice(lastSpace, i - lastSpace);
i = lastSpace;
lastSpace = -1;
lineWidths.push(lastLineWidth);
maxLineWidth = Math.max(maxLineWidth, lastLineWidth);
line++;
pos.x = 0;
pos.y += data.lineHeight;
prevCharCode = null;
continue;
}
var charData = data.chars[charCode];
if(!charData) continue;