Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text gameobjects: Word wrap doesn't take into account letter spacing #7002

Open
Stever1388 opened this issue Jan 9, 2025 · 0 comments
Open

Comments

@Stever1388
Copy link

Version

  • Phaser Version: 3.86.0
  • Operating system: Windows
  • Browser: Chrome

Description

If you set the new letter spacing property to a Phaser.GameObject.Text object that also has word wrap, the wrapping doesn't take into account the new letter spacing, which means the object can become wider than the desired max word wrap width.

Example Test Code

import Phaser from 'phaser';

export default class LetterSpacingWordWrapIssue extends Phaser.Scene {
  create() {
    const longString = 'This is very long text that should wrap to another line but when letter spacing is applied, the wrapping doesn\'t get adjusted.';
    const style = {
      align: 'center',
      color: '#ffffff',
      wordWrap: {
        width: 250
      }
    };

    // add a rect behind the text to show where the bounds should be
    const rect = this.add.rectangle(this.scale.width / 4, this.scale.height / 4, 250, 250, 0xffffff, 0).setStrokeStyle(2, 0xff0000);
    this.text = this.add.text(rect.x, rect.y, longString, style).setOrigin(0.5);
    rect.setSize(250, this.text.height);

    // add a rect behind the text to show where the bounds should be
    const rectForLetterSpacing = this.add.rectangle(this.scale.width / 4 * 3, this.scale.height / 4, 250, 250, 0xffffff, 0).setStrokeStyle(2, 0xff0000);
    this.textWithLetterSpacing = this.add.text(rectForLetterSpacing.x, rectForLetterSpacing.y, longString, style).setOrigin(0.5)
      .setLetterSpacing(10); // note: letter spacing is missing from the style config
    rectForLetterSpacing.setSize(250, this.textWithLetterSpacing.height);

    // add a rect behind the text to show where the bounds should be
    const rectForLetterSpacingNeg = this.add.rectangle(this.scale.width / 4, this.scale.height / 4 * 3, 250, 250, 0xffffff, 0).setStrokeStyle(2, 0xff0000);
    this.textWithLetterSpacingNeg = this.add.text(rectForLetterSpacingNeg.x, rectForLetterSpacingNeg.y, longString, style).setOrigin(0.5)
      .setLetterSpacing(-2); // note: letter spacing is missing from the style config
    rectForLetterSpacingNeg.setSize(250, this.textWithLetterSpacingNeg.height);
  }
}

Result:
image

Additional Information

The Phaser.Types.GameOjbects.Text.TextStyle doesn't have a letterSpacing property in it, but it does have the lineSpacing property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant