Help with textbox

Guys
Guys how can I make text boxes like discord, where if there is no space to type more it drops down(increasing the text box size).



Also want to increase size if it doesn’t fit anymore

It will be great if u help me

1 Like

You would want to use the TextBounds property or/and the TextFits property and adjust the size accordingly.

1 Like

You can use TextService:GetTextSize() to check how long should the textbox be in Y axis:

local Textbox = script.Parent
local TextService = game:GetService("TextService")
local OriginalYSize = Textbox.AbsoluteSize.Y
Textbox:GetPropertyChangedSignal("Text"):Connect(function()
	local RequiredSize = TextService:GetTextSize(Textbox.Text,Textbox.TextSize,Textbox.Font,Vector2.new(Textbox.AbsoluteSize.X,1080))
	if RequiredSize.Y > OriginalYSize then
		Textbox.Size = UDim2.new(Textbox.Size.X.Scale,Textbox.Size.X.Offset,Textbox.Size.Y.Scale,RequiredSize.Y+2)
	else
		Textbox.Size = UDim2.new(Textbox.Size.X.Scale,Textbox.Size.X.Offset,Textbox.Size.Y.Scale,OriginalYSize)
	end
end)

Edit: Fixed some parts of the code so it works properly.

4 Likes

You could use:

Which gets the size of text from a string, fontsize, font and maximum frame size

1 Like

Thank you very much, I will definitely try it.

Thank you for your,It worked fine.
But,I wanna know what is happening in the code could u please help me

i want to know why did u put the 4th parameter y value 1080

Okay let me explain it to you:

local Textbox = script.Parent
Assigns the Textbox variable to script.Parent object.

local TextService = game:GetService("TextService")
Pretty much does the same thing above but it assigns the TextService variable to the Text Service.

local OriginalYSize = Textbox.AbsoluteSize.Y
Saves the original Y axis length to OriginalYSize variable so when we calculate the required length later it doesn’t shrink smaller than it’s original size.

Textbox:GetPropertyChangedSignal("Text"):Connect(function()
We want to run the function inside :Connect() only when the Text property of the Textbox changes so that’s why I used :GetPropertyChangedSignal("Text") because it’s more efficient than using the .Changed event since it only fires on a single specified property change rather than every property change.

local RequiredSize = TextService:GetTextSize(Textbox.Text,Textbox.TextSize,Textbox.Font,Vector2.new(Textbox.AbsoluteSize.X,1080))
TextService:GetTextSize() is a function of text service that calculates how many pixels the text will cover up with the specified parameters, 1st parameter takes the string which in this case the text inside the textbox. 2nd parameter takes how big the text font will be which in this case textbox’s TextSize property. 3rd parameter takes which font the text will be which in this case textbox’s Font property. 4th parameter takes what’s the pixel limit of this text which takes a Vector2 value which is why I crated a new Vector2 value there with Vector2.new(Textbox.AbsoluteSize.X,1080) instead of using the TextBounds property of the textbox because when the calculated text lenght goes beyond the TextBounds limit we provided it just returns the TextBound limit instead of returning required TextBound length so we need to give something a little bit bigger than the original size. We know that we need to keep the X axis length constant so I just used Textbox.AbsoluteSize.X and for the Y axis, I put 1080 pixels just because I wanted to, you can change it to something else. So like that we got the Vector2 value of the required size of the textbox.`

if RequiredSize.Y > OriginalYSize then
	Textbox.Size = UDim2.new(Textbox.Size.X.Scale,Textbox.Size.X.Offset,Textbox.Size.Y.Scale,RequiredSize.Y+2)
else
	Textbox.Size = UDim2.new(Textbox.Size.X.Scale,Textbox.Size.X.Offset,Textbox.Size.Y.Scale,OriginalYSize)
end

This if statement check if required size Vector2 value’s Y property is bigger than the original Y value, if it is then it changes the textbox’s Y.Offset value to the new Y value. If it isn’t then it changes Y.Offset value to the original.

I hope this explanation helps.

2 Likes

It Helped a looot, thank u very much again