Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UI Panel that is sized vertically by content - TextMeshPro sizing not working

Discussion in 'UGUI & TextMesh Pro' started by theDawckta, Aug 14, 2018.

  1. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    50
    I need a pop up anchored at the bottom of the screen that will adjust it's height based on the text inside of it. I saw this https://docs.unity3d.com/Manual/HOWTO-UIFitContentSize.html but it doesn't seem to help.

    My set up so far
    1. Add Canvas to scene
    2. Add Panel to Canvas
    3. Add TextMeshPro - Text to Panel
    4. Make the Panel's Y pivot 0
    5. Add Content Size Fitter to Panel, change Horizontal Fit and Vertical Fit to preferred size
    6. Add Horizontal Layout Group to Panel
    7. Add enough text to TextMeshPro - Text to make it two lines.

    From here I think I have tried every combination of child size/expand options for the horizontal layout group but the text just ends up running outside the screen.



    I need TextMeshPro - Text to be constrained by the Panel so it is forced to two lines which forces the panel to adjust it's height from the bottom anchor point.

    Can anyone show me the correct way to do this?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Make sure you are using the TextMeshProUGUI text component as the normal TextMeshPro component doesn't work with Canvas layout components.
     
  3. theDawckta

    theDawckta

    Joined:
    Jun 16, 2014
    Posts:
    50
    Verified, I am using TextMeshProUGUI. I selected UI -> TextMeshPro - Text when I added it to the hierarchy.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    By design, preferred width returns the width required to display all the text without any width restrictions. It doesn't take into consideration its parent width for example.

    See the following post which includes a potential way to limit the width of a RectTransform.