Skip to content

Commit f5cae5a

Browse files
committed
Added in Text justification feature. Fix phaserjs#4291
1 parent 61827f3 commit f5cae5a

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/gameobjects/text/TextStyle.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,14 +949,16 @@ var TextStyle = new Class({
949949
},
950950

951951
/**
952-
* Set the text alignment.
953-
*
954-
* Expects values like `'left'`, `'right'`, `'center'` or `'justified'`.
952+
* Set the alignment of the text in this Text object.
953+
*
954+
* The argument can be one of: `left`, `right`, `center` or `justify`.
955+
*
956+
* Alignment only works if the Text object has more than one line of text.
955957
*
956958
* @method Phaser.GameObjects.TextStyle#setAlign
957959
* @since 3.0.0
958960
*
959-
* @param {string} align - The text alignment.
961+
* @param {string} [align='left'] - The text alignment for multi-line text.
960962
*
961963
* @return {Phaser.GameObjects.Text} The parent Text object.
962964
*/

src/gameobjects/text/static/Text.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,16 @@ var Text = new Class({
946946
},
947947

948948
/**
949-
* Set the text alignment.
950-
*
951-
* Expects values like `'left'`, `'right'`, `'center'` or `'justified'`.
949+
* Set the alignment of the text in this Text object.
950+
*
951+
* The argument can be one of: `left`, `right`, `center` or `justify`.
952+
*
953+
* Alignment only works if the Text object has more than one line of text.
952954
*
953955
* @method Phaser.GameObjects.Text#setAlign
954956
* @since 3.0.0
955957
*
956-
* @param {string} align - The text alignment.
958+
* @param {string} [align='left'] - The text alignment for multi-line text.
957959
*
958960
* @return {Phaser.GameObjects.Text} This Text object.
959961
*/
@@ -1216,6 +1218,33 @@ var Text = new Class({
12161218
{
12171219
linePositionX += (textWidth - textSize.lineWidths[i]) / 2;
12181220
}
1221+
else if (style.align === 'justify')
1222+
{
1223+
// To justify text line its width must be no less than 85% of defined width
1224+
var minimumLengthToApplyJustification = 0.85;
1225+
1226+
if (textSize.lineWidths[i] / textSize.width >= minimumLengthToApplyJustification)
1227+
{
1228+
var extraSpace = textSize.width - textSize.lineWidths[i];
1229+
var spaceSize = context.measureText(' ').width;
1230+
var trimmedLine = lines[i].trim();
1231+
var array = trimmedLine.split(' ');
1232+
1233+
extraSpace += (lines[i].length - trimmedLine.length) * spaceSize;
1234+
1235+
var extraSpaceCharacters = Math.floor(extraSpace / spaceSize);
1236+
var idx = 0;
1237+
1238+
while (extraSpaceCharacters > 0)
1239+
{
1240+
array[idx] += ' ';
1241+
idx = (idx + 1) % (array.length - 1 || 1);
1242+
--extraSpaceCharacters;
1243+
}
1244+
1245+
lines[i] = array.join(' ');
1246+
}
1247+
}
12191248

12201249
if (this.autoRound)
12211250
{

src/gameobjects/text/typedefs/TextStyle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @property {number} [strokeThickness=0] - The thickness of the stroke around the Text. Set to zero for no stroke.
1414
* @property {Phaser.Types.GameObjects.Text.TextShadow} [shadow] - The Text shadow configuration object.
1515
* @property {Phaser.Types.GameObjects.Text.TextPadding} [padding] - A Text Padding object.
16-
* @property {string} [align='left'] - The alignment of the Text. This only impacts multi-line text.
16+
* @property {string} [align='left'] - The alignment of the Text. This only impacts multi-line text. Either `left`, `right`, `center` or `justify`.
1717
* @property {integer} [maxLines=0] - The maximum number of lines to display within the Text object.
1818
* @property {number} [fixedWidth=0] - Force the Text object to have the exact width specified in this property. Leave as zero for it to change accordingly to content.
1919
* @property {number} [fixedHeight=0] - Force the Text object to have the exact height specified in this property. Leave as zero for it to change accordingly to content.

0 commit comments

Comments
 (0)