I am trying to make a textbox. I don’t need wrapping of text onto several lines, I just need one line.
I have an expanding box that wraps nicely around my text, and that works.
But what I want now is to limit the maximum width of the box, and let TextMeshPro add the Ellipsis to shorten the text.
LayoutElement does not give me a maximum width, only minimum or preferred.
How can I achieve this effect?
By textbox do you mean a text input box or something else? If you mean text input See the setup produced by creating object “UI/Input Field(text mesh pro)” in the menu for creating object not component.
Do you want the box to tightly wrap around text for something like drawing a border or background around text? If you are fine with the box with border always being max width ignore the following, there are simpler ways to achieve. Otherwise keep reading.
See this forum post Limit Max Width of Layout Component? I recently used the solution suggested by Stephan-B. Haven’t tried the custom layout component scripts suggested by others, so can’t tell how well those work. It discusses a slightly more complex situation where you want expanding up to max width and afterwards wrapping. But disabling wrapping and changing overflow to ellipsis should give the result you expect.
Just messed with it and slightly simplified setup that I could make was something like this:
* Box 1 (vertical layout[control child width=on, other flags off]) - width of this controls max width
- Box 2 (vertical layout[control child width=on, other flags off]) - draws tight box around text if you need it
^ TextMeshPro(wrapping=disabled, overflow=Ellipsis)
No content size fitters. You might want to consider also enabling Control child size height=On, Child force expand height=On so that you only have to set height for top level box instead of all the intermediate elements.
One of the downsides of this approach is that dimensions of top level Box1 is the maximum size not current size of Box2. This may complicate the code if you have some extra code for positioning box 1.
If anyone knows simpler setups I would be happy to hear about them as well.