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

Expand TextMeshPro / Container automatically

Discussion in 'UGUI & TextMesh Pro' started by Goty-Metal, Feb 24, 2021.

  1. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    So i know you can set a Text Mesh Pro to auto-size the font to fit it's parent container BUT what i want is the opposite, to keep a font size and expand the parent/s to fit the text, this is my text panel, i use it mainly for tooltips/show info (first image).

    The second image is how i'd like it to work (fit container to text mesh pro size/lines).

    And the third image shows how it works and how it should work.

    I've tried messing around with vertical/horizontal layouts, content fiters, layout elements... with no luck.

    Hope to find some help here, thanks!
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      24.4 KB
      Views:
      302
    • 2.PNG
      2.PNG
      File size:
      12.3 KB
      Views:
      292
    • 3.PNG
      3.PNG
      File size:
      42.8 KB
      Views:
      293
  2. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    Hi again, i'm still trying to do this :(
    Help is <3
     
  3. zeinging

    zeinging

    Joined:
    Jul 14, 2019
    Posts:
    2
    I just found a way to change the rectTransform to the overflowing text size.

    public RectTransform LineOneText;

    if(LineOneText.GetComponent<TMPro.TMP_Text>().isTextOverflowing){//change rectTransform only if text overflows
    Vector2 temp = LineOneText.sizeDelta;
    temp.y = LineOneText.GetComponent<TMPro.TMP_Text>().preferredHeight;
    LineOneText.sizeDelta = temp;
    }


    though you could also just do it like this instead:

    //change rectTransform to preferredHeight
    Vector2 temp = LineOneText.sizeDelta;
    temp.y = LineOneText.GetComponet<TMPro.TMP_Text>().preferredHeight;
    LineOneText.sizeDelta = temp;
     
  4. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    You don't need any code to do this. Put Tmp Text as a child of the game object with a Layout Group and Content Fitter components. Choose Preferred Size for horizontal/vertical. In combination with Layout Element and Preferred Width/Height you'll have full control of the parent's size
     
    zeinging likes this.