How to get the number of lines in a GUI.Label by setting a max width.

If I have alot of other code but the main portion im trying to debug is this. I have a singleton class called FontManager which is FM.BalloonStyle.

private Rect TextRect = new Rect();
public string BalloonText = string.empty;
private GUIStyle BalloonTextStyle = FM.instance.BalloonStyle;
GUI.Label(TextRect, BalloonText, BalloonTextStyle);

So how can i get how many numbers of line of text are in the Label?

You can use GUIStyle.CalcHeight to determine the height of the text in the Label.

You could then divide that by GUIStyle.lineHeight to see how many lines there are.

I haven't tried the second part before, but I don't see why it wouldn't work.


    var theContent  :  GUIContent  =  GUIContent(BalloothText);
    var theWidth    :  float       =  TextRect.width;

    var textHeight  =  BalloonTextStyle.CalcHeight(theContent, theWidth);

    var numLines    =  textHeight / BalloonTextStyle.lineHeight;

Somewhere, where balloonText is set, it’s probably being sliced into equal width lines, and has the count computed. Could just save that (if it isn’t already.)

If they are using
in balloonText, could also just count the number of those, plus one.