I want to create UI text elements with backgrounds that resize to fit around the text. I’ve tried to follow guides which use vertical layout groups to achieve this. What I have at the moment partially works. The image and text resize as more text is added. But the height of the background image won’t shrink below 110 (see top text).
I can see that the preferred height of the background image is 110, but I’m not sure what determines this. When I try using a LayoutElement to change the Preferred Height, this breaks the auto-resizing behaviour that I want.
The UI hierarchy is:
“Contents Container” with VerticalLayoutGroup options enabled:
- Control Child Size: Width + Height
- Child Force Expand: Width.
“Heading Background” with VerticalLayoutGroup options enabled:
- Control Child Size: Width + Height
“Heading Text” using TextMeshProUGUI.