Skip to content

Font Extensions

Shmellyorc edited this page Aug 31, 2026 · 1 revision

FontExtensions provides convenience extension methods for measuring text dimensions using a font.


Overview

Feature Description
Measure Width Get the width of text in pixels
Measure Height Get the height of text in pixels

Methods

MeasureWidth

Measures the width of the specified text when rendered with this font.

Font font = AssetManager.Instance.Load<SpriteFont>("fonts/arial.png");

float width = font.MeasureWidth("Hello World");
// Returns the width in pixels

MeasureHeight

Measures the height of the specified text when rendered with this font.

Font font = AssetManager.Instance.Load<SpriteFont>("fonts/arial.png");

float height = font.MeasureHeight("Hello World");
// Returns the height in pixels

Examples

Text Layout

public void LayoutText(Font font, string text, Vect2 position)
{
    // Measure the text
    float width = font.MeasureWidth(text);
    float height = font.MeasureHeight(text);
    
    // Center the text
    Vect2 centeredPos = new Vect2(
        position.X - width / 2f,
        position.Y - height / 2f
    );
    
    // Draw centered text
    _batcher.DrawText(font, text, centeredPos, Color.White);
}

Dynamic Button Sizing

public Rect2 CreateButton(Font font, string text, Vect2 position)
{
    float padding = 10f;
    float width = font.MeasureWidth(text) + padding * 2f;
    float height = font.MeasureHeight(text) + padding * 2f;
    
    return new Rect2(position, new Vect2(width, height));
}

Text Alignment

public void DrawAlignedText(Font font, string text, Rect2 bounds, TextAlignment alignment)
{
    float textWidth = font.MeasureWidth(text);
    float textHeight = font.MeasureHeight(text);
    
    Vect2 position = bounds.Position;
    
    switch (alignment)
    {
        case TextAlignment.Center:
            position += new Vect2(
                (bounds.Width - textWidth) / 2f,
                (bounds.Height - textHeight) / 2f
            );
            break;
        case TextAlignment.Right:
            position += new Vect2(
                bounds.Width - textWidth,
                (bounds.Height - textHeight) / 2f
            );
            break;
        // ... other alignments
    }
    
    _batcher.DrawText(font, text, position, Color.White);
}

Truncation Check

public string TruncateText(Font font, string text, float maxWidth)
{
    if (font.MeasureWidth(text) <= maxWidth)
        return text;
    
    // Truncate with ellipsis
    string ellipsis = "...";
    float ellipsisWidth = font.MeasureWidth(ellipsis);
    float availableWidth = maxWidth - ellipsisWidth;
    
    for (int i = text.Length; i > 0; i--)
    {
        string truncated = text.Substring(0, i);
        if (font.MeasureWidth(truncated) <= availableWidth)
            return truncated + ellipsis;
    }
    
    return ellipsis;
}

Comparison

Method Description
font.Measure(text).X Gets the width of the text
font.MeasureWidth(text) Same as above, more readable
font.Measure(text).Y Gets the height of the text
font.MeasureHeight(text) Same as above, more readable

Summary

Method Description
MeasureWidth Gets the width of text in pixels
MeasureHeight Gets the height of text in pixels

Back to Home

Clone this wiki locally