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

Sizing font correctly

Discussion in 'UI Toolkit' started by unitydungeonmas, Nov 1, 2020.

  1. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    Is there a way to auto-size the font to fit a label? Right now things get cropped off.
     
  2. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    this used to be possible with "autosize" option i think
     
  3. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    754
  4. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,218
    Not quite the same thing, but I do adjustment to the width of headers and labels and such by counting the characters of the longest string then multiplying it by an estimate of how wide in pixels each character is (I am sure there is a way to know the exact value, I usually just use 6-7) and then adjust the width of the header/label by the value I come up with. I end up with consistent and accurate automatic adjustment doing this.

    I am guessing that you could do something similar to set the font size. Instead of going by character count for width, you could use 'label.resolvedStyle.height (or width)' to get the actual size of the element you want to match and then come up with a quick calculation based on whatever you want to be the basis for the font size adjustment, be it width or height, and then just set your font based on that.

    Code (CSharp):
    1. private void OnEnable()
    2. {
    3.     psRoot = rootVisualElement;  
    4.    
    5.     //  This is fired off after the layout phase but before the first frame, so you can get accurate
    6.     //  measurements of the current elements but avoid things "popping" into place at the first frame draw
    7.     psRoot.RegisterCallback<GeometryChangedEvent>(ExecuteDeferred);
    8.     // [...]
    9. }
    10.  
    11. private void ExecuteDeferred(GeometryChangedEvent evt)
    12. {
    13.     psRoot.UnregisterCallback<GeometryChangedEvent>(ExecuteDeferred);
    14.  
    15.     // Takes all keys in Dictionary and counts the number of characters
    16.     // in the string then returns the count of the longest string
    17.     var elementKeys = customPackages.Keys.ToList();
    18.     var labelWidth = elementKeys
    19.         .Select(x => x)
    20.         .Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur).Length;
    21.  
    22.     // Multiply the number of characters by the approximate width in
    23.     // pixels of the characters font size + a small padding amount
    24.     psPackageSelectorHeader.style.width = labelWidth * 7 + 15;
    25. }
     
    unitydungeonmas likes this.
  5. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    thx, i just want to get something working for beta
     
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    (EDIT: this was in response to the measuring-text-size reply)

    I've been searching for this all day, and it seems the answer is:

    "UIToolkit cannot do this, but core Unity can, so use Unity to do it for you, and use the result inside UIToolkit"

    The magic class you need is https://docs.unity3d.com/ScriptReference/TextGenerator.html

    ...you still have to figure out what the font is, what the fontsize is. But you are probably specifying those manually anyway
     
    unitydungeonmas likes this.
  7. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    do u have example of this ty
     
  8. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    i just want tmpros autosize, even a hack to get sumting working