How to customize the width of a EditorGUILayout.LabelField?

When creating custom inspectors or editors how can I edit the LabelField width property?

Asked by blademan @

Blademaster Answer

Oddly enough, what you need to pass is a GUILayout.Width as the GUIStyle parameter. The straightforward option is to just call the GUILayout.Width static method and it will return the apppriate GUILayoutOption.

public class CustomEditor : Editor {

    public override void OnInspectorGUI() {
        EditorGUILayout.LabelField("Top", GUILayout.Width(20));
    }

}
You can also be more flexible and call one of the following methods:
  • MinWidth which lets you set a minimum width.
  • MaxWidth allows you to set a maximum width.
  • ExpandWidth will allow the element to take all the available space.

© Bladecast 2018. All rights reserved.