Calculating Label size depending on a string length [solved]

Started by mode7, Sun 14/11/2010 16:14:55

Previous topic - Next topic

mode7

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?

Calin Leafshade

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.

monkey0506

Do something like this:

Code: ags
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);

mode7

Thanks monkey- that sounds good. I'll try it right away

Khris

Note that in the case of an outlined font you have to calculate the width of the outline font instead.

mode7


SMF spam blocked by CleanTalk