Whats the formula coded in a textbox for the wrap text feature

im trying to make a custom text editor using image labels as characters

its all works untill it gets to sizing. when i try to resize the text, is all jumbled

I wouldnt never had to go thought this if the had the built in textbox didnt show html tags when editing with rich text

this is my code

local TextBox = script.Parent
local doc = game.Players.LocalPlayer.PlayerGui.Document
wait(2)
local cursor = game.CollectionService:GetTagged("Cursor")[1]
local max = 72

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
end)

TextBox.Focused:Connect(function()
	doc.Background.ScrollingFrame.Paper.WordProccessor.Enabled = false
end)

TextBox.FocusLost:Connect(function()
	doc.Background.ScrollingFrame.Paper.WordProccessor.Enabled = true
	if tonumber(TextBox.Text) >= max then
		TextBox.Text = tostring(max)
	end
	local length = 0
	for i, letter in pairs(game.CollectionService:GetTagged("Selected")) do
		letter.TextSize = tonumber(script.Parent.Text) * 2
		length += (game.TextService:GetTextSize(letter.Text,letter.TextSize,letter.Font,letter.AbsoluteSize).X/letter.AbsoluteSize.X)
		for i, char in pairs(game.CollectionService:GetTagged("Character")) do
			char.PositionOrder.Value = char.AbsolutePosition.X
			if char.Parent == cursor.Parent then
				if char.Position.X.Offset >= cursor.Position.X.Offset then
					letter.Position = UDim2.new(0,letter.Position.X.Offset+length,0,0)
				end                                                                         ----- Nudges all characters on the right a step to fit the new character
			end
		end
	end
end)