remove empty space between the EditorGUILayout.TextField's label and inputbox

Hi.
I am trying to create an editorwindow menu but I have a problem with the EditorGUILayout.TextField.

My code is something like this:

void OnGUI () 
{
 EditorGUILayout.BeginHorizontal();
 {
  EditorGUILayout.TextField("mytextfield", value);
  GUILayout.Space(10);
  EditorGUILayout.Toggle("myToggle", true);
 }
 EditorGUILayout.EndHorizontal();
}

As you see, something very simple.
I can control the space between the Textfiled and the toggle by changing the GUILayout.Space(x) line but I cannot find where to change the space beween the EditorGUILayout.TextField’s label and inputbox.
Same problem with the toggle’s name and checkbox too.
It looks like there is 2 tabulations inbetween but I would like to remove this.

You can use this method to get the label size auto adjusted

/// <summary>
  /// Create a EditorGUILayout.TextField with no space between label and text field
  /// </summary>
  public static string TextField(string label, string text)
  {
  var textDimensions = GUI.skin.label.CalcSize(new GUIContent(label));
  EditorGUIUtility.labelWidth = textDimensions.x;
  return EditorGUILayout.TextField(label, text);
  }
1 Like

thanks. I’ll use your code to update mine.

Thanks,It’s solved my problem.

It is very useful

Thanks, this worked well for me. And to control the width of the input field you can add a third parameter to the return statement as show here:

return EditorGUILayout.TextField( label, text,  GUILayout.Width( 400 ) );

Beware, I observed you need to reset the value via

 EditorGUIUtility.labelWidth = -1;

Otherwise all future labels within the inspector will have the width of your first label.

I moved this necroed thread from 2014 to the correct forum (IMGUI).