Change DrawRect lenght according to text lenght

Wanna know how to make DrawRect lenght change according to the text lenght.

How it is : [ some ] text
appears out of the rect

How it should be : [ some text ]
appears inside of the rect

if you’re using a panel,

Panel:GetTextSize

if not, you might be interested in knowing that putting # before a string variable will tell you how many symbols it contains

so

local blah = “cats”
print(#blah)

it prints 4

Alternatively to the post above, if you’re just looking to get the width/height of a string, you can use

surface.GetTextSize

There are some good examples at the bottom of that page.

I have progress, my friends ! thanks for the help Moat and bletotum :slight_smile:

[editline]11th November 2016[/editline]

Okay, i have a problem.
Whenever i hover over a swep with description the size changes, anyway to fix this ?

Code?


hook.Add("HUDPaint" , "DrawHud", function() --hpbar/armorbar

	local ply = LocalPlayer()
	local health = ply:Health()
	local armor = ply:Armor()
	

	local width1 = surface.GetTextSize( ply:getDarkRPVar( "job" ) )
	local width2 = surface.GetTextSize( ply:getDarkRPVar( "money" ) )
	local width3 = surface.GetTextSize( ply:getDarkRPVar( "salary" ) )

	if !ply:Alive() then
		return
	end

	surface.SetDrawColor( Color( 255, 255, 255, 255 ))
	surface.DrawOutlinedRect( ScrW() - ScrW() + 30.47, ScrH() - 50, 200 + 6, 30 + 6 )
	draw.RoundedBox(0, ScrW() - ScrW() + 31, ScrH() - 48, 200 + 4, 30 + 2, Color(0, 0, 0, 225)) -- health
	draw.RoundedBox(0, ScrW() - ScrW() + 38, ScrH() - 45, health * 1.9, 26, Color(200, 0, 0)) -- health
	
	surface.SetDrawColor( Color( 255, 255, 255, 255 ))
	surface.DrawOutlinedRect( ScrW() - ScrW() + 250, ScrH() - 50, 200 + 6, 30 + 6 )
	draw.RoundedBox(0, ScrW() - ScrW() + 251.34, ScrH() - 48, 200 + 4, 30 + 2, Color(0, 0, 0, 225)) -- armor
	draw.RoundedBox(0, ScrW() - ScrW() + 258, ScrH() - 45, armor * 1.9, 26, Color(0, 80, 200)) -- armor

	draw.RoundedBox(0, ScrW() - ScrW() + 30.54, ScrH() - 76, width1 + 30.54 * 1.55 + 2, 19, Color(0, 0, 0, 225))
	draw.RoundedBox(0, ScrW() - ScrW() + 30.54, ScrH() - 99, width2 + 30.54 * 1.87, 19, Color(0, 0, 0, 225))
	draw.RoundedBox(0, ScrW() - ScrW() + 30.54, ScrH() - 122, 99, 19, Color(0, 0, 0, 225))
	surface.SetDrawColor( Color( 255, 255, 255, 255 ))
	surface.DrawOutlinedRect( ScrW() - ScrW() + 30.54, ScrH() - 76, width1 + 30.54 * 1.6 + 3, 20 )
	draw.DrawText("Trabalho: ".. ply:getDarkRPVar( "job" ), "Font", ScrW() / 50, ScrH() - 73, Color(255, 255, 255))
	surface.DrawOutlinedRect( ScrW() - ScrW() + 30.54, ScrH() - 99, width2 + 30.54 * 1.9, 20 )
	draw.DrawText("Carteira: ".. ply:getDarkRPVar( "money" ), "Font", ScrW() / 50, ScrH() - 96, Color(0, 240, 0))
	surface.DrawOutlinedRect( ScrW() - ScrW() + 30.54, ScrH() - 122, 93, 20 )
	draw.DrawText("Salário: ".. ply:getDarkRPVar( "salary" ), "Font", ScrW() / 50, ScrH() - 119, Color(0, 50, 255))
end)

Okay, two things: Firstly, you’re setting the font AFTER getting the text size. You should be doing:



surface.SetFont("Font")


Before all your width variables are created. Secondly, you’re not including the strings you’re adding on, like "Trabalho: ". You need to include them, so it’s more like this:



surface.SetFont("Font")
local width1 = surface.GetTextSize( "Trabalho: ".. ply:getDarkRPVar( "job" ) )
local width2 = surface.GetTextSize( "Carteira: ".. ply:getDarkRPVar( "money" ) )
local width3 = surface.GetTextSize( "Salário: ".. ply:getDarkRPVar( "salary" ) )


[editline]11th November 2016[/editline]

The reason you need to set the font before getting the text size is because it can’t possibly figure out what font you’re about to use, or what string you’re about to use before actually doing it. If you set both of these, you should be getting the right size.

It works ! thanks MPan for the help :slight_smile: