Skip to content

Commit fbdf807

Browse files
committed
Add "maxLines" style attribute to Text object, maximum number of lines
to be shown for wrapped text or 0 for no limit. (default).
1 parent 268b133 commit fbdf807

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/gameobjects/Text.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
3535
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
3636
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
37+
* @param {number} [style.maxLines=0] - The maximum number of lines to be shown for wrapped text.
3738
* @param {number} [style.tabs=0] - The size (in pixels) of the tabs, for when text includes tab characters. 0 disables. Can be an array of varying tab sizes, one per tab stop.
3839
*/
3940
Phaser.Text = function (game, x, y, text, style) {
@@ -284,6 +285,7 @@ Phaser.Text.prototype.setShadow = function (x, y, color, blur, shadowStroke, sha
284285
* @param {number} [style.strokeThickness=0] - A number that represents the thickness of the stroke. Default is 0 (no stroke).
285286
* @param {boolean} [style.wordWrap=false] - Indicates if word wrap should be used.
286287
* @param {number} [style.wordWrapWidth=100] - The width in pixels at which text will wrap.
288+
* @param {number} [style.maxLines=0] - The maximum number of lines to be shown for wrapped text.
287289
* @param {number|array} [style.tabs=0] - The size (in pixels) of the tabs, for when text includes tab characters. 0 disables. Can be an array of varying tab sizes, one per tab stop.
288290
* @return {Phaser.Text} This Text instance.
289291
*/
@@ -300,6 +302,7 @@ Phaser.Text.prototype.setStyle = function (style) {
300302
style.strokeThickness = style.strokeThickness || 0;
301303
style.wordWrap = style.wordWrap || false;
302304
style.wordWrapWidth = style.wordWrapWidth || 100;
305+
style.maxLines = style.maxLines || 0;
303306
style.shadowOffsetX = style.shadowOffsetX || 0;
304307
style.shadowOffsetY = style.shadowOffsetY || 0;
305308
style.shadowColor = style.shadowColor || 'rgba(0,0,0,0)';
@@ -371,7 +374,13 @@ Phaser.Text.prototype.updateText = function () {
371374
var maxLineWidth = 0;
372375
var fontProperties = this.determineFontProperties(this.style.font);
373376

374-
for (var i = 0; i < lines.length; i++)
377+
var drawnLines = lines.length;
378+
if (this.style.maxLines > 0 && this.style.maxLines < lines.length)
379+
{
380+
drawnLines = this.style.maxLines;
381+
}
382+
383+
for (var i = 0; i < drawnLines; i++)
375384
{
376385
if (tabs === 0)
377386
{
@@ -428,7 +437,7 @@ Phaser.Text.prototype.updateText = function () {
428437

429438
// Calculate text height
430439
var lineHeight = fontProperties.fontSize + this.style.strokeThickness + this.padding.y;
431-
var height = lineHeight * lines.length;
440+
var height = lineHeight * drawnLines;
432441
var lineSpacing = this._lineSpacing;
433442

434443
if (lineSpacing < 0 && Math.abs(lineSpacing) > lineHeight)
@@ -439,7 +448,7 @@ Phaser.Text.prototype.updateText = function () {
439448
// Adjust for line spacing
440449
if (lineSpacing !== 0)
441450
{
442-
height += lineSpacing * lines.length;
451+
height += lineSpacing * drawnLines;
443452
}
444453

445454
this.canvas.height = height * this._res;
@@ -472,7 +481,7 @@ Phaser.Text.prototype.updateText = function () {
472481
this._charCount = 0;
473482

474483
// Draw text line by line
475-
for (i = 0; i < lines.length; i++)
484+
for (i = 0; i < drawnLines; i++)
476485
{
477486
// Split the line by
478487

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,6 +4704,7 @@ declare module Phaser {
47044704
strokeThickness?: number;
47054705
wordWrap?: boolean;
47064706
wordWrapWidth?: number;
4707+
maxLines?: number;
47074708
shadowOffsetX?: number;
47084709
shadowOffsetY?: number;
47094710
shadowColor?: string;

0 commit comments

Comments
 (0)