GUIStyle.wordWrap
seems to have a bug that makes the container not shrink onto its content when set to true
. The shrinking part is done by applying the two following things to the content and parent wrapper.
- smallTextStyle.stretchWidth = false;
- GUILayout.ExpandWidth(false)
* You can see usage in the code snippet below
It seems that wordWrap causes a layout bug where it adds extra space as if you were using the default font size and style. You can see this in the image with the line comparing the width. If you use a bigger size than the default with word wrap it just wraps it at the width it would have been at the default size.
I submitted a bug report but I was hoping for some help finding more evidence, wondering if anyone has run into this issue, etc. I am using Unity 4.5.0f6.
You can run the small example that demonstrates the problem. Just put this block in a Editor window OnGUI. Toggle comment out the wordWrap on the text style to see the effect.
GUIStyle smallTextStyle = new GUIStyle();
smallTextStyle.fontSize = 7;
smallTextStyle.fontStyle = FontStyle.Bold;
smallTextStyle.wordWrap = true; // Comment out this line to see the effect toggle on and off
Color smallTextColor = new Color(1f, 1f, 1f);
smallTextStyle.normal.textColor = smallTextColor;
smallTextStyle.stretchWidth = false;
smallTextStyle.stretchHeight = false;
GUIStyle wrapperStyle = new GUIStyle();
wrapperStyle.stretchWidth = false;
wrapperStyle.stretchHeight = false;
Texture2D wrapperTex = new Texture2D(1, 1);
wrapperTex.SetPixels(new Color[] { new Color(0f, 0f, 0f, .7f) });
wrapperTex.Apply();
wrapperStyle.normal.background = wrapperTex;
GUILayout.BeginVertical(wrapperStyle, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
{
GUILayout.Label(new GUIContent("Heya, see me here and that extra space over that shouldn't be there"), smallTextStyle, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
}
GUILayout.EndVertical();