Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: mode7 on Sun 14/11/2010 16:14:55

Title: Calculating Label size depending on a string length [solved]
Post by: mode7 on Sun 14/11/2010 16:14:55
Hi guys,

I'm drawing a speech bubble on a gui. The text is put into a text label. I'm dynamically drawing the size of the textbox depending on the length of the string.

 int chars;
 int lines;

 while (text.Length >= chars) {
   
   lines++;
   chars = chars+26;
 }
 gbubble.Height = 13*lines;// 13 is the height of the font
 
The problem is that this is unreliable, sometimes I end up with spare lines and sometime a word gets cut off.
Has anyone an idea of a more precise way of doing that?
Title: Re: Calculating Label size depending on a string length
Post by: Calin Leafshade on Sun 14/11/2010 16:18:59
Theres a function that calculates the width of a string in pixels based on the font but i cant remember exactly what it is.. check the manual.
Title: Re: Calculating Label size depending on a string length
Post by: monkey0506 on Sun 14/11/2010 18:57:45
Do something like this:

int width = GetTextWidth(text, gbubble.Font); // is gbubble the GUI or the label? If it's not the label replace it here with the label's name
gbubble.Height = GetTextHeight(text, gbubble.Font, width);
Title: Re: Calculating Label size depending on a string length
Post by: mode7 on Sun 14/11/2010 22:59:41
Thanks monkey- that sounds good. I'll try it right away
Title: Re: Calculating Label size depending on a string length
Post by: Khris on Mon 15/11/2010 04:54:43
Note that in the case of an outlined font you have to calculate the width of the outline font instead.
Title: Re: Calculating Label size depending on a string length
Post by: mode7 on Mon 15/11/2010 21:46:20
Works smooth as silk! thanks guys