How do I detect if text doesn't fit in text container

0 favourites
  • 5 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • How do I detect if text doesn't fit in text container?

    For example, I am making a level editor where players can define what NPCs will say. I can limit the number of characters in the input text box but I would rather make it so the text display sequentially, but I need a way to determine if characters fit the text area or not.

    I am using Spritefont but also welcome ideas with the normal text object.

    Any ideas?

  • I'm going to bump this rather than ask the same question, as it'd be good to know.

    My game has text that can over run. I want the scale to change if this occurs, but I have no way of measuring if it does.

  • Looks like it's impossible. Text really is the most limited part of Construct 2, isn't it? Can't change colours/fonts... can't see what it looks like in game, and need to play the game to just see if it's correct...

    And finally, if your text ever fills up the box, you can't check it and change the scale accordingly... you just have to settle for an average font size and hope it all works out well. Shame.

  • Have a look at the SpriteFont.TextHeight expression. If it's less than SpriteFont.Height then all the text fits in the box.

    To set the scale to fit all the text you could do an iterative approach with a loop. Basically it tries a scale of 1.0, sees if the text fits and if it does stop the loop. If it doesn't

    repeat 100 times

    ---> spritefont: set scale to (100-loopindex)/100

    --- spritefont: height > spritefont.textheight

    ------> stop loop

    Here's an example: ... t_fit.capx

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could also get a good estimate by calculating the width from the character widths and (either mean or max) spacing/kerning, especially for the sprite font object, I would assume. If you use a monospaced font even easier of course.

    I'm not sure if spritefont behaves like the text object in the way that it only knows the text width and height 1 frame AFTER the text was set. But I have hopes, that it works instantly. It if does, an iterative approach like Rojohound mentioned is probably the easiest way to go about it .


    Saw the second post. It looks like it works instantly.

    So you can do this:

    • Spritefont set Scale to 1
    • Spritefont set text to yourtext
    • Spritefont set scale to clamp(spritefont.Width/spritefont.TextWidth,0,1)

    (1 being the zoom factor you want at most)

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