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

Get help using Construct 2

Post » Mon Aug 18, 2014 8:11 pm

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?
B
63
S
12
G
6
Posts: 456
Reputation: 8,981

Post » Thu Jan 14, 2016 10:13 pm

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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Sat Jan 16, 2016 7:00 pm

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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Sat Jan 16, 2016 10:00 pm

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:
https://dl.dropboxusercontent.com/u/542 ... t_fit.capx
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Sat Jan 16, 2016 10:17 pm

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 .

edit:
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)
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786


Return to How do I....?

Who is online

Users browsing this forum: unicornspitfire and 63 guests