- Home /
Based on text content increase container size
For my game, I am using new Unity UI. I have question regarding this.
I want to increase my container size based on more number of characters enter into text area. So based on text, I want to increase container size. Following image represent my question clearly.
Please give me your suggestion.
I have tried with content size fitter but cant able to increase size of container.
Ah yes you actually need a vertical or horizontal layout on the parent container AND a content size fitter as specified here("Fit to size of UI element with child Text" section).
You can then use the padding parameters of the to adjust the margins around your text.
Answer by amanpu · Sep 22, 2015 at 11:11 AM
You should use Content Size Filter along with Horizontal Layout Group. For this follows the following steps:
Add Canvas -> Image (Container of your text)
Add Text and make it child of Image
Add Content Size Filter and Horizontal Layout Group components to Image
Select preferred size in both Horizontal fit and Vertical fit fields in Content Size Filter
Hope this helps
If you want more fine-grained controll than a Content Size Fitter, you can ask the text for it's preferred width and height with the preferredWidth and preferredHeight properties.
@Baste I couldn't find preferredWidth and preferredHeight properties, Can you help more?
Ok I found them, but how to make use of them as they are read only
You don't want to set them - you want to read them and then resize whatever container you're using to fit the text.
Say you want to have a lot wider margins than what the Content Size Fitter gives you. Then you could reference the preferredWidth of your text object, and set the container to have that width pluss your custom margins.
You could probably fix that by adding invisible panels on both sides of the text - this is just a different way of doing the same thing.