Wrapping text with 'draw.DrawText'

Is it possible to wrap the text automatically to fit the boundaries of the current item?

Code Snippet:

[lua]
local TestingPanel = vgui.Create( “DPanel”, ChangeTeamSheet )
TestingPanel:SetPos( ScrW()*0.11, ScrH()*0.01 )
TestingPanel:SetSize( ScrW()*0.62, ScrH()*0.21 )
TestingPanel.Paint = function() – Paint function
–Set our rect color below us; we do this so you can see items added to this panel
draw.RoundedBox( 8, 0, 0, TestingPanel:GetWide(), TestingPanel:GetTall(), Color(50, 50, 50, 255) ) – Draw the rect
draw.DrawText("Description: "… TeamDescription[ply:Team()], “LargeFont”, TestingPanel:GetWide()*0.01, TestingPanel:GetTall()*0.05, Color(255, 255, 255, 255),TEXT_ALIGN_LEFT)
end
[/lua]

Unfortunately, I believe this wouldn’t work without you manually adding newlines to the string that will be printed. Last I checked, draw.DrawText() doesn’t really have overflow detection and I think it just cuts off the remaining letters that don’t fit into its width and height parameters.

You could, however:

  1. string.format() that string you’re printing
  2. Run it through a function that takes the width and height of the panel into consideration along with your personal calculation of how many words can safely fit on each line
  3. The function returns your string with appropriate newlines after the max amount of words that can fit on the line are reached throughout the whole string.

There may be an issue if the panel is resizeable, but there’s always a calculation or else we wouldn’t have properly-functioning text panels.

Okay, thanks for your help but I managed to get it working in the end.

If anyone want’s the completed testing and working code, here it is:

[lua]
surface.CreateFont( “Ariel”, 48, 500, true, true, “LargeFont” )
surface.CreateFont( “Ariel”, 24, 500, true, true, “MediumFont” )
surface.CreateFont( “Ariel”, 16, 500, true, true, “SmallFont” )

UText = {}

function UpdateText(t)
	for i=1,4 do
		UText* = vgui.Create("DTextEntry", TestingPanel*)
		UText*:SetPos(ScrW()*0.005,ScrH()*0.005)
		UText*:SetSize(TestingPanel*:GetWide() - 10, TestingPanel*:GetTall() - 10)
		UText*:SetMultiline(true)
		UText*:SetWrap(true)
		if i == 1 then
			if t == 0 then
				UText*:SetText("Job Title: ".. team.GetName(t) .. "

Salary: "… TeamSalary[t] )
else
UText*:SetText("Job Title: “… team.GetName(t) … "
Salary: $”… TeamSalary[t] )
end
elseif i == 2 then
UText*:SetText("Description:
"… TeamDescription[t])
elseif i == 3 then
UText*:SetText("Rules:
"… TeamRules[t])
elseif i == 4 then
UText*:SetText("Extra Information:
" … TeamExtraInfo[t])
end
UText*:SetTextColor(color_white)
UText*:SetFont(“MediumFont”)
UText*:SetDrawBackground(false)
UText*:SetDrawBorder(false)
UText*:SetEditable(false)
end
end
hook.Add(“TextUpdate”, “Update the text!”, UpdateText)

hook.Call("TextUpdate", GAMEMODE, LocalPlayer():Team())

[/lua]