- Home /
Change gui box height based on text content?
Hi All,
I have variable length text files. My requirement is the GUI box length/height should change based on the text content such that the text content should fit within the box. And also the text content in the box should get aligned. Suggest any ideas.
Thanks in advance.
Answer by ArkaneX · Jan 21, 2014 at 05:30 PM
If the width of the box is known, then you can use GUIStyle.CalcHeight. If you want to calculate width as well, then you have to use GUIStyle.CalcMinMaxWidth first. Please note that the second method will return different width values only if your style has word wrapping enabled. Text alignment can be set in your GUI style in the inspector, or programmatically by GUIStyle.alignment.
Word of warning: avoid modifying default style, like
GUI.skin.box.alignment = TextAnchor.MiddleCenter;
because this is not reversible (or at least not straightforward). If you want to play with style properties, just create a new style and configure it in the inspector. Or alternatively, create new one in code using default style as constructor parameter:
var myStyle = new GUIStyle(GUI.skin.box);
Thanks for your reply. The above mentioned text files are Questions.
So can you suggest me a way to provide answers.The answers are of two types,one is of four options to choose one correct answer and the other is of match the following type(the user should match the images with the text).
In first case I would probably set the answers width to a fixed value, depending on screen width, and for each answer calculate the height using CalcHeight method. And then, position the answers vertically according to calculated heights.
As another option, if your answers are short, you can display them in two rows and two columns format (like in Who Wants to Be a $$anonymous$$illionaire)
And regarding the image/text matchig, I think you can use the same formats. Depending on the size of the image, it might be a good idea to display it on the left, while possible matches on the right.
But ultimately it's up to you - just try a few options, ask your friends and see which layout they like.