- Home /
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?
Answer by jahroy · Dec 08, 2011 at 12:05 AM
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;
Answer by Owen-Reynolds · Dec 08, 2011 at 01:59 PM
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 \n in balloonText, could also just count the number of those, plus one.
Your answer
Follow this Question
Related Questions
[SOLVED]GUIStyle Word Wrap Check 1 Answer
GUI.Label or any text with GUIStyle as Texture 1 Answer
GUI.CalcSize returning incorrect values 0 Answers
Defualt GUI Style 1 Answer
Fading a text gui style 3 Answers