• 2D
  • How to make the size of the TextEdit change as the text increases

i want the TextEdit change rect_size.y when then text increases. so i use the code

func _on_Control_text_changed():
	rect_size.y = get_line_count()*line_min_y

but when i let 'wrap_enabled' be true(enables text wrapping when it goes beyond the edge of what is visible)

it has some problem,because the text wrapping is not a new line, Does not increase the count(get_line_count())

Is there any good way to solve my problem(the TextEdit change rect_size.y when then text increases.)

i need your help please,thanks a lot. BR.

In order to better illustrate the problem, I provide a demo as attach

so to my understanding this is actually expected behaviour. If you tick the option show_line_numbers you can see that actually when text wrapping kicks in, no new line is added.

One idea that i have on how to still solve your issue: Use a monospaced font (each character occupies the same amount of space) then you can calculate the number of lines from the width of your TextEdit and the width of a character (trial and error on exact value for width of character) then connect the signal text_changed to your TextEdit node inside the signal handler you can then set the correct rect.size

Thank you very much for your solution, I will try it, but if the engine can provide the actual number of rows I will feel better

i find font class has a function is get_wordwrap_string_size, but the way counts the number of lines is different from textedit

Then I read the source code, I saw a function get_total_visible_rows in textedit can solve my problem.but it in c++,Not bound to gdscript.

I will bind it, compile the engine, and submit an interface request on github

thanks your answer.

wow that was a deep dive you made! glad to hear you found a sustainable solution from which other can also profit!

6 months later

@vmjcv Hello, sorry to bother you. My project depends only on in your solution here.

I'm a beginner, and I would like to know how to implement get_total_visible_rows in my code. What should I do to access that function?

5 months later

@BlokyMose said: @vmjcv Hello, sorry to bother you. My project depends only on in your solution here.

I'm a beginner, and I would like to know how to implement get_total_visible_rows in my code. What should I do to access that function?

bind it, compile the engine