static Rect MeasureText( Sandbox.Rect& position, string text, string fontFamily, float fontSize, float fontWeight, TextFlag flags )
static Rect MeasureText( Sandbox.Rect& position, Sandbox.TextRendering/Scope& scope, TextFlag flags )

book_4_sparkGenerated
code_blocksInput

Description

The MeasureText method in the Sandbox.Graphics class is used to calculate the bounding rectangle of a given text string when rendered with specified font properties. This method is useful for determining how much space a text string will occupy on the screen, allowing for precise layout and alignment in graphical applications.

Usage

To use the MeasureText method, you need to provide the following parameters:

  • position: A reference to a Sandbox.Rect that will be updated with the calculated size and position of the text.
  • text: The string of text you want to measure.
  • fontFamily: The name of the font family to use for rendering the text.
  • fontSize: The size of the font in points.
  • fontWeight: The weight of the font, typically a value between 100 and 900.
  • flags: A Sandbox.TextFlag enumeration value that specifies additional text rendering options, such as alignment and wrapping.

The method returns a Sandbox.Rect that represents the bounding box of the text.

Example

// Example usage of MeasureText
Sandbox.Rect textRect = new Sandbox.Rect();
string text = "Hello, World!";
string fontFamily = "Arial";
float fontSize = 12.0f;
float fontWeight = 400.0f;
Sandbox.TextFlag flags = Sandbox.TextFlag.Left;

Sandbox.Rect measuredRect = Sandbox.Graphics.MeasureText(ref textRect, text, fontFamily, fontSize, fontWeight, flags);

// Use measuredRect to position or align other UI elements based on the text size.