-
Notifications
You must be signed in to change notification settings - Fork 1
Font Extensions
Shmellyorc edited this page Aug 31, 2026
·
1 revision
FontExtensions provides convenience extension methods for measuring text dimensions using a font.
| Feature | Description |
|---|---|
| Measure Width | Get the width of text in pixels |
| Measure Height | Get the height of text in pixels |
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 pixelsMeasures 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 pixelspublic 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);
}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));
}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);
}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;
}| 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 |
| Method | Description |
|---|---|
MeasureWidth |
Gets the width of text in pixels |
MeasureHeight |
Gets the height of text in pixels |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions