- Home /
Resize text rect
I would like to calculate the height of a box filled with text from a string. This needs to be calculated every time because different lengths of string variables can be passed into it. When I fill my scrolling window with the text, I would like it's height to resize so that there is not too much empty space after the text ends or that it is too short it cuts the text off before completing.
I have a string of text, a rect with a particular width, and the font size.
Using GUIStyle.CalcHeight tells me that my image length is much shorter than what it actually needs to be. I haven't been able to find any other functions in Unity that could help me reach the answer I am looking for.
Anyone know a good equation or shortcut that I can use to get this right?
Answer by Waz · Jan 17, 2013 at 11:21 PM
Perhaps you are looking for GUIStyle.CalcSize. Ensure the GUIStyle has the font set beforehand.
Turns out (of course!) that FancyLabels actually has a function in it that gives me the exact number of lines of text within the Rect I've established for it.
A little guesswork to get the right font size * the number of lines and I got the exact value!
Your answer
![](https://koobas.hobune.stream/wayback/20220613091121im_/https://answers.unity.com/themes/thub/images/avi.jpg)