• Wrapping text with 'draw.DrawText'
    2 replies, posted
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. [U][B]You could, however:[/B][/U] 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[i] = vgui.Create("DTextEntry", TestingPanel[i]) UText[i]:SetPos(ScrW()*0.005,ScrH()*0.005) UText[i]:SetSize(TestingPanel[i]:GetWide() - 10, TestingPanel[i]:GetTall() - 10) UText[i]:SetMultiline(true) UText[i]:SetWrap(true) if i == 1 then if t == 0 then UText[i]:SetText("Job Title: ".. team.GetName(t) .. "\nSalary: ".. TeamSalary[t] ) else UText[i]:SetText("Job Title: ".. team.GetName(t) .. "\nSalary: $".. TeamSalary[t] ) end elseif i == 2 then UText[i]:SetText("Description:\n".. TeamDescription[t]) elseif i == 3 then UText[i]:SetText("Rules:\n".. TeamRules[t]) elseif i == 4 then UText[i]:SetText("Extra Information:\n" .. TeamExtraInfo[t]) end UText[i]:SetTextColor(color_white) UText[i]:SetFont("MediumFont") UText[i]:SetDrawBackground(false) UText[i]:SetDrawBorder(false) UText[i]:SetEditable(false) end end hook.Add("TextUpdate", "Update the text!", UpdateText) hook.Call("TextUpdate", GAMEMODE, LocalPlayer():Team()) [/lua]
Sorry, you need to Log In to post a reply to this thread.