Text Area: size (height) to fit text, via code

I’m updating a TextArea programmatically, from a data table. Of course, when this happens, the TextArea size remains fixed. Which is exactly right, in some cases.

In other cases, the TextArea should grow/shrink to match the new text. Is there a way for my code to do that?

Ooh, nice idea! You can approximate it by counting how many characters are in the TextArea, based on how long its text is and how many characters fit on a line (and how many \n characters are in it). You can then use this to update the TextArea’s height property (which is in pixels).

I’ll also note this as a feature request :slight_smile:

That looks tricky. In a long paragraph, it’s going to change from one line to the next, unless I use a monospaced font. And even then, it’s going to change dynamically, as the user resizes their browser window.

As an interim step, perhaps it would be best for me to out-and-out replace the existing TextArea with a brand new one. I’ll try it.

Good point! I’ve baked this into the next version of Anvil. In the next 24-48 hours, you will notice that TextAreas now have an auto_expand property :smiley:

1 Like

You’ve beat me to it! Thank you!

Naming suggestion: auto_size or size_to_fit (since it will auto-shrink as well as auto-expand).

Actually, it won’t auto-shrink to smaller than the height specified in the designer, so I’m going to stick with auto_expand :slight_smile:

Thanks for clarifying.:slightly_smiling_face: