surface.GetTextSize

From GMod Wiki

Jump to: navigation, search
Function
Syntax surface.GetTextSize( String Text )
Where is this used?
Description:
Gets the height and width of a string of text. Only works if the font is already set.
Returns: Integer Width, Integer Height
Part of Library: Surface
Realm: NewerClient.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=Surface.GetTextSize]Surface.GetTextSize [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]


Examples

DescriptionChecks the Length of a player name using a Medium Size font. If its too long it switches to a smaller Font.
Used onNewerClient.png
Code
 
	surface.CreateFont( "Arial", 21, 500, true, false, "MedFont" )
	surface.CreateFont( "Arial", 18, 500, true, false, "SmallFont" )
	surface.SetFont("MedFont")
	local PlayerName = LocalPlayer():GetName()
	Width, Height = surface.GetTextSize(PlayerName)
	if Width < 165 then
		draw.DrawText(  PlayerName,
			"MedFont",
			100, ScrH()-35,
			Color(255, 255, 255, 255),
			1)
	else
	draw.DrawText(  PlayerName,
			"SmallFont",
			100, ScrH()-35,
			Color(255, 255, 255, 255),
			1)
	end
 
OutputIf the PlayerName using MedFont is wider than 165pixels, use smaller font.


Additional Notes

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox