- Home /
[SOLVED]GUIStyle Word Wrap Check
Hey everyone!
I have custom GUIStyle set up to use wordwrap and I was wondering if there is anyway to check to see if a Label gets wrapped so it will move the next label below it out of the way.
I want to maintain good spacing between the lines that get displayed. Am I going about this the wrong way?
Here is an example of my issue:
Here is my code if that helps at all!
GUI.Label(new Rect(Screen.width/1.9f, hieght, 400, 30),question1, Question);
GUI.Label(new Rect(Screen.width / 1.9f, hieght + 50, 500, 50), answer1, Answer);
GUI.Label(new Rect(Screen.width / 1.9f, hieght + 150, 500, 50), question2, Question);
GUI.Label(new Rect(Screen.width / 1.9f, hieght + 200, 500, 50), answer2, Answer);
GUI.Label(new Rect(Screen.width / 1.9f, hieght + 300, 500, 50), question3, Question);
GUI.Label(new Rect(Screen.width / 1.9f, hieght + 350, 500, 50), answer3, Answer);
Thanks for looking!
EDIT: Final Code
void OnGUI()
{
GUI.skin = Skin;
GUIStyle Question = Skin.customStyles[0];
GUIStyle Answer = Skin.customStyles[1];
if (isOn == true)
{
string question1 = getQuestion1();
string question2 = getQuestion2();
string question3 = getQuestion3();
string answer1 = getAnswer1();
string answer2 = getAnswer2();
string answer3 = getAnswer3();
hieght = (Screen.height / 4);
GUILayout.BeginArea(new Rect(Screen.width / 1.9f, hieght, 400, 1000));
//GUI.BeginGroup(new Rect(Screen.width/1.9f, hieght, 100, 100));
GUILayout.Label(question1, Question);
GUILayout.Space(10);
GUILayout.Label(answer1, Answer);
GUILayout.Space(30);
GUILayout.Label(question2, Question);
GUILayout.Space(10);
GUILayout.Label(answer2, Answer);
GUILayout.Space(30);
GUILayout.Label(question3, Question);
GUILayout.Space(10);
GUILayout.Label(answer3, Answer);
GUILayout.EndArea();
//GUI.EndGroup();
}
The GUILayout class will space itself automatically based on settings you apply to it. I don't know of a way to detect word wrapping though.
Answer by Mortoc · Nov 28, 2012 at 10:28 PM
You can use the GUIStyle.CalcHeight function for this: http://docs.unity3d.com/Documentation/ScriptReference/GUIStyle.CalcHeight.html
I'm assuming that the variable 'Answer' is your GUIStyle and 'answer1' is your string. You can figure out the different sizes of the typed in text something like this:
float oneLineHeight = Answer.CalcHeight(new GUIContent(""), 500);
float answerHeight = Answer.CalcHeight(new GUIContent(answer1), 500);
Yes this does work, however i like GUILayout much more ;) It automatically sizes every element but you can override most things if you want. See the last 8 functions of GUILayout
Agreed, especially for a simple GUI layout. I generally use GUI for pixel perfect things like HUDs. GUILayout works better for simple layouts like this. I'd go with GUILayout if I were you paprocjo.
Ok! I am looking into GUILayout as we speak! I will report back later! Thanks for pointing me in the right direction!
Your answer
Follow this Question
Related Questions
ArgumentException: Getting control 1's position... 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GUILayout.Label - WordWrap and FontSize issue. 2 Answers
Can you use GUIStyle with GUILayout? 0 Answers