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

Reducing space between TextField and its label

Discussion in 'UI Toolkit' started by XGT08, Jun 9, 2020.

  1. XGT08

    XGT08

    Joined:
    Aug 1, 2013
    Posts:
    1,845
    Hello,

    Is there a way to reduce the space between a text field and its associated label?

    So, if I create a text field like this:
    Code (CSharp):
    1. var textField = new TextField();
    2. textField.label  = "Label";
    3. // ...
    The space between the label and the actual field is way too big. I tried messing with the style margin proeprties but it doesn't seem to work.

    Thanks,
    Andrew
     
  2. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    183
    The TextField is composed of a Label and TextInput. The Label is responsible for defining that width in your case. You can change this using either way:

    1) via C# code:

    var label = textField.Q<Label>(className: TextField.labelUssClassName);
    label.style.minWidth = ...; // some specific value like 100f;


    2) via USS (suppose your TextField has name "myTextField"):

    myTextField > unity-text-field__label {
    min-width: 100px;
    }
     
    MurphyMurph_21 likes this.
  3. XGT08

    XGT08

    Joined:
    Aug 1, 2013
    Posts:
    1,845
    Thanks for the tip :)

    It seems however, that it doesn't work. I am using the C# version and it doesn't seem to have any effect. I assume also, that it is the maxWidth property that I should be looking at. Since I would like to put a cap on how big it gets. But still, it does not work. No biggie, it's ok.
     
  4. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    183
    Oh sorry you might also have textField.style.width fixed by code or a selector. Try to check what causes the value to be fixed by using the UIElements Debugger. iirc maxWidth is not usually set for a TextField. On my side, when I test this all I get is a min-width that comes by default and that is all.
     
  5. cfviper

    cfviper

    Joined:
    Aug 12, 2020
    Posts:
    1
    Might be a different use case, but I had the spacing issue when adding a TextField to a custom toolbar.
    After reading this thread, I eventually tried:

    Code (CSharp):
    1. var myTextField = new TextField();
    2. // use any needed myTextField  properties
    3.  
    4. VisualElement ve = myTextField;
    5. ve.ElementAt(0).style.minWidth = 10;

    This worked for me.
     
    abreu9999 likes this.
  6. roryo

    roryo

    Joined:
    May 21, 2009
    Posts:
    1,479
    @cfviper - that's a clever solution. I was having the same wide label problem with FloatField and your solution worked there as well.

    Still, I am hoping that Unity has provided an easier way to do this using USS. Consider the case where you have 20 such fields. It seems cumbersome to have to add a style adjustment for each one.

    I saw that there is a static member FloatField.labelUssClassName mentioned in the scripting manual. I logged it to find that the classname is "unity-float-field__label"

    I tried setting this in a .uss file using:

    Code (CSharp):
    1.  
    2. .unity-text-field__label
    3. {
    4.     width: 15;
    5.     minWidth: 15;
    6.     maxWidth: 15;
    7. }
    I was hoping that it would set the width of every float field, but it did not seem to have any effect.
     
  7. KSTC-LYB

    KSTC-LYB

    Joined:
    Jun 8, 2022
    Posts:
    1
    .unity-property-field__label {
    width: 100px;
    min-width: 100px;
    max-width: 100px;
    }
     
    MurphyMurph_21 likes this.