Search Unity

Make a label's width match its content?

Discussion in 'Editor & General Support' started by G4M5T3R, Nov 16, 2019.

  1. G4M5T3R

    G4M5T3R

    Joined:
    Jan 10, 2015
    Posts:
    10
    Not entirely sure where to post this... UI seemed right until I read the stickies.
    This is a non-issue really but I'm new to custom editors and none of my attempts to figure this out have worked.
    I can ignore this small issue but would prefer not to. So my question is: How do I make a label's width match its content?

    I making a custom editor for a scriptable object. The editor has a Horizontal group that contains 2 controls. An IntField and a TextField, with labels, representing an ID and Name respectively. It looks like this:

    I simply want the fields to hug the labels. In other words, I want the label's width to match its content.

    Setting GUILayout.ExpandWidth(false) only seems to affect the field's width and not the width of the label.
    Using PrefixLabel breaks drag-to-change value functionality. I don't "Need" that functionality but it'd be nice to keep. However, a prefix takes in a GUIStyle anyway, and not GUILayoutOptions. So I can't even use ExpandWidth(false) here. Naturally I tried to make a new guiStyle to get around this but that didn't work as intended either. Lastly, I tried a skin with my style and setting GUI.skin to my skin and so on but isn't this is overkill? Regardless that didn't work either. So I'm either missing something or doing it wrong.

    Code (CSharp):
    1.  
    2. myScriptableObj so;
    3. //GUIStyle myStyle;
    4. public void OnEnable()  {
    5.         so = (myScriptableObj)target;
    6. }
    7.  
    8. //public void OnGUI() {
    9.         //myStyle = new GUIStyle(GUI.skin.label);
    10.         //myStyle.margin = new RectOffset(someRect);
    11.         //myStyle.padding = new RectOffset(someRect);
    12. }
    13.  
    14. public override void OnInspectorGUI() {
    15.         EditorGUILayout.BeginHorizontal();
    16.         //EditorGUILayout.PrefixLabel("#", myStyle);
    17.         so.objID = EditorGUILayout.IntField("#", so.objID, GUILayout.ExpandWidth(false));
    18.         //EditorGUILayout.PrefixLabel("Name", myStyle);
    19.         so.objName = EditorGUILayout.TextField("Name", so.objName, GUILayout.ExpandWidth(false));
    20.         EditorGUILayout.EndHorizontal();
    21. }
    22.  

    Any insight would be appreciated. Is GUISkin the right appraoch? What about a Content Fitter? How do I even create one in a custom editor if applicable here?
     
    Last edited: Nov 16, 2019
  2. G4M5T3R

    G4M5T3R

    Joined:
    Jan 10, 2015
    Posts:
    10
    This might not be the best solution but after a little more reading I realized I could just use GUILayout.Label for the label instead of a EditorGUILayout method for the label and unlike PrefixLabel it CAN take in LayoutOptions! It works, and it looks like this:

    Code (CSharp):
    1.  
    2. EditorGUILayout.BeginHorizontal();
    3. GUILayout.Label("#");
    4. EditorGUILayout.IntField(someInt);
    5. GUILayout.Label("Name");
    6. EditorGUILayout.TextField(someString);
    7. EditorGUILayout.EndHorizontal();
    8.  
     
  3. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,263
    For what it's worth, you can also get the size of a GUIContent (rect?) in any given GUIStyle through:

    Code (CSharp):
    1. float width = EditorStyles.label.CalcSize(new GUIContent("Label")).x