Garry's Mod Wiki

Revision Difference

surface.GetTextSize#528780

<function name="GetTextSize" parent="surface" type="libraryfunc"> <description>Returns the width and height (in pixels) of the given text, but only if the font has been set with <page>surface.SetFont</page>.</description> <realm>Client and Menu</realm> <args> <arg name="text" type="string">The string to check the size of.</arg> </args> <rets> <ret name="" type="number">Width of the provided text</ret>⤶ <ret name="" type="number">Height of the provided text</ret>⤶ <ret name="" type="number">Width of the provided text.</ret>⤶ <ret name="" type="number">Height of the provided text.</ret>⤶ </rets> </function> <example> <description>Prints out the size of "Hello World" in the Trebuchet24 font.</description> <description>Prints out the size of `Hello World` in the Trebuchet24 font.</description> <code> surface.SetFont("Trebuchet24") local message = "Hello World" local width, height = surface.GetTextSize(message) print("Text width: " .. width .. ", text height: " .. height) </code> <output>Text width: 100, text height: 24</output> ⤶ </example>⤶ ⤶ ⤶ ⤶ ⤶ <example>⤶ </example>⤶ ⤶ <example>⤶ <description>Get text height very quickly. No tables or spare variables used.</description> <code> local message = "Hello World" surface.SetFont("Trebuchet24") local height = select(2, surface.GetTextSize(message)) print(height) </code> <output>24</output> ⤶ </example></example>