Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Overlapping labels on same row

Discussion in 'Immediate Mode GUI (IMGUI)' started by vividhelix, Feb 13, 2015.

  1. vividhelix

    vividhelix

    Joined:
    Mar 20, 2013
    Posts:
    78
    Hi there,

    I'm trying to display two labels on the same row, with the left one occupying as much space as needed and the right one occupying the rest. Left one is left aligned, right one is right aligned.

    Here's how it looks (I reversed the rendering order so that at the very least the left one is on top): http://imgur.com/21wWR4H - see how the 3rd and 4th rows overlap.

    This is my current code:

    Code (CSharp):
    1.  
    2. private static readonly GUIStyle richTextGuiStyle = new GUIStyle { richText = true, fontSize = 12, margin = new RectOffset(5, 5, 5, 5), normal = { textColor = EditorStyles.label.normal.textColor } };
    3. private static readonly GUIStyle rightAlignRichTextGuiStyle = new GUIStyle { richText = true, fontSize = 12, margin = new RectOffset(5,5,5,5),
    4. ...
    5. var path = x.ParentPath;
    6. GUILayout.BeginHorizontal(regularLineGuiStyle);
    7. GUILayout.Label(path, rightAlignRichTextGuiStyle);
    8. var lastRect = GUILayoutUtility.GetLastRect();
    9. var iconRect = new Rect(lastRect);
    10. iconRect.width = iconRect.height;
    11. GUI.DrawTexture(iconRect, GetIconForFile(x.Name), ScaleMode.ScaleToFit);
    12. var textRect = new Rect(lastRect);
    13. textRect.xMin = textRect.xMin + textRect.height;
    14. textRect.width = textRect.width - textRect.height;
    15. GUI.Label(textRect, Highlight(x.Name, itemName), richTextGuiStyle);
    16. GUILayout.EndHorizontal();
    I tried GUIStyle.clipping on the right one but it had no effect. What am I missing?
     
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    The result you are looking for can simply be done by using FlexiableSpace and ExpandWidth equal to false.

    Code (CSharp):
    1.  
    2.     GUILayout.BeginHorizontal();
    3.     {
    4.       GUILayout.Label( "Left Aligned", GUILayout.ExpandWidth(false) );
    5.       GUILayout.FlexibleSpace();
    6.       GUILayout.Label( "Right Aligned", GUILayout.ExpandWidth( false ) );
    7.     }
    8.     GUILayout.EndHorizontal();
    9.  
     
    vividhelix likes this.
  3. vividhelix

    vividhelix

    Joined:
    Mar 20, 2013
    Posts:
    78
    Awesome, this does indeed work. Thanks!
     
    BMayne likes this.