Dynamic Text Font Size

  • Hello! I have a window that has to remain a fixed size. This window is going to be populated with text using the Text Object. I want the text font size to change based on how many characters there are dynamically. Whats the best way to achieve this?

    Thanks a lot and Happy New Year! :)

  • You can set the text box size to the text pixel size plus add a margin amount.

    TextWidth

    TextHeight

    Return the size of the actual text content within the text object's rectangle. Note the Text object must be drawn before these have the correct values. If you set the text then immediately access these expressions, they won't yet have the correct value. Adding a Wait 0.1 seconds action will usually solve this.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for your reply!

    You recommend changing the text box size based on the volume? I cannot change the box sixe because it is a part of a fixed-size menu.

    I have to find a way to reduce font size based on character count or something similar

  • Oh i see.

    Normally in games with a limited text box size, like a dialogue box, they show a bit of text then the player clicks and it deletes the old text and shows some more.

    An alternative is to have a scroll box.

    scirra.com/tutorials/1217/custom-scrollbar-or-slider

    Use "force own texture" and "destination out" to hide text outside of the scroll box area.

    But if you want to change the text size to always fit i guess you'd have to set the text, then get the text width, see if its greater than the window size, if it is then resize the text and check it again until it fits. You could do this in a loop normally so it appeared to happen instantly however its a bit tricky with the text object as it doest update until the next game loop, hence the wait 0.1 action.

  • Okay thank you very much for your suggestions. I have opted to go for a scroll bar! :)

  • if you want the font size to increment based on how many letters or characters are in the text field

    you can simply use, len(Text.Text) that gives u the number of characters and then divide or multiply those by 2 or 4 based on how much you want the font to increase and set the resulting value as the new font.size.

    and there are some useful stuff inside here also that might help you, text-character-width-in-pixels

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)