Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUIStyle.wordWrap Bug with non-default Font Size: Parent wrapper does not shrink to content

Discussion in 'Immediate Mode GUI (IMGUI)' started by MLM, Aug 15, 2014.

  1. MLM

    MLM

    Joined:
    Jun 11, 2013
    Posts:
    6
    `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.

    Code (csharp):
    1.  
    2. GUIStyle smallTextStyle = new GUIStyle();
    3. smallTextStyle.fontSize = 7;
    4. smallTextStyle.fontStyle = FontStyle.Bold;
    5. smallTextStyle.wordWrap = true; // Comment out this line to see the effect toggle on and off
    6. Color smallTextColor = new Color(1f, 1f, 1f);
    7. smallTextStyle.normal.textColor = smallTextColor;
    8. smallTextStyle.stretchWidth = false;
    9. smallTextStyle.stretchHeight = false;
    10.  
    11. GUIStyle wrapperStyle = new GUIStyle();
    12. wrapperStyle.stretchWidth = false;
    13. wrapperStyle.stretchHeight = false;
    14. Texture2D wrapperTex = new Texture2D(1, 1);
    15. wrapperTex.SetPixels(new Color[] { new Color(0f, 0f, 0f, .7f) });
    16. wrapperTex.Apply();
    17. wrapperStyle.normal.background = wrapperTex;
    18.  
    19. GUILayout.BeginVertical(wrapperStyle, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
    20. {
    21.     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));
    22. }
    23. GUILayout.EndVertical();
    24.  
     
    Last edited: Aug 15, 2014
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,306
    Code (csharp):
    1.  
    2. GUIContent label = new GUIContent("Heya, see me here and that extra space over that shouldn't be there");
    3.  
    4. GUILayout.Label(label, smallTextStyle, GUILayout.Width(smallTextStyle.CalcSize(label).x), GUILayout.ExpandHeight(false));
    5.  
     
  3. MLM

    MLM

    Joined:
    Jun 11, 2013
    Posts:
    6
    Manually setting the width does work. But the beauty of the GUI Groups like `BeginVertical`, `BeginScrollView`, etc is to have a wrapper around whatever you put inside without worrying about what is inside.

    This wrapper shrink to content functionality is equivalent to `display: inline-block` in HTML, CSS. Demo: http://jsfiddle.net/6Lwf0b4h/
     
  4. MLM

    MLM

    Joined:
    Jun 11, 2013
    Posts:
    6
    This has been confirmed as a bug by the Unity Team. You can go vote on this bug on the Issue Tracker!
     
    Last edited: Aug 21, 2014