Know when a text mesh pro's text changed

I’m trying to dynamically resize a text mesh pro’s rect transform based on its text, the same way an html div works, and I can’t find a way to do it, and I’m starting to think there’s no way.
I’ve tried using an an input field instead of the regular text mes pro ugui component, but the onValueChanged event is fired only if the value changes from inside the game, which doesn’t help me.

I’m kind of surprised this isn’t a more widely requested feature, but oh well.

Is there any way to achieve this?

Thanks!

I think you will want to use TMP_Text.GetPreferredValues(). It returns the width and height needed to contain the textmesh. You can use these values to set the transform of the parent. I think you would probably want to assign the returned Vector2 directly to the transform’s localScale property.

You may also want to use TMPro_EventManager.TEXT_CHANGED_EVENT to detect when changes occur.