Skip to content

TextHelper

Shmellyorc edited this page Aug 31, 2026 · 2 revisions

TextHelper provides comprehensive text manipulation utilities including wrapping, truncation, measurement, and formatting for UI and rendering systems.


Overview

Feature Description
Word Wrapping Wrap text at word boundaries to fit a width
Character Wrapping Wrap text character by character
Truncation Truncate text with ellipsis at the end or middle
Text Cleaning Normalize line endings and remove trailing whitespace
Measurement Measure text width and wrapped height
Time Formatting Format seconds to M:SS or H:MM:SS
Indentation Wrap text with indentation for subsequent lines

Text Wrapping

WrapText

Wraps text to fit within a specified width, breaking at word boundaries when possible.

string longText = "This is a very long sentence that needs to be wrapped to fit within a specific width.";
string wrapped = TextHelper.WrapText(font, longText, 200f);

WrapTextCharacter

Wraps text character by character to fit within a specified width.

string wrapped = TextHelper.WrapTextCharacter(font, longText, 200f);

WrapTextWithIndent

Wraps text with a specified indentation for subsequent lines.

string indented = TextHelper.WrapTextWithIndent(
    font,
    longText,
    maxWidth: 200f,
    indentWidth: 20f
);

WrapTextToLines

Wraps text and returns an array of lines.

string[] lines = TextHelper.WrapTextToLines(font, longText, 200f);
foreach (var line in lines)
{
    // Process each line
}

Truncation

TruncateWithEllipsis

Truncates text with ellipsis to fit within a specified width.

string truncated = TextHelper.TruncateWithEllipsis(
    font,
    "This is a very long text that needs truncation.",
    100f
);
// Returns: "This is a very..."

EllipsizeMiddle

Truncates text with ellipsis in the middle, useful for file paths.

string path = TextHelper.EllipsizeMiddle(
    font,
    "C:/Users/Username/Documents/Games/MyGame/Content/textures/player.png",
    150f
);
// Returns: "C:/Users/.../player.png"

Text Cleaning

CleanText

Cleans text by normalizing line endings and removing trailing whitespace.

string dirtyText = "Hello   \n\n  World  \r\n";
string cleaned = TextHelper.CleanText(dirtyText);
// Returns: "Hello\nWorld"

Measurement

MeasureWrappedHeight

Measures the height of text after wrapping to a specified width.

float height = TextHelper.MeasureWrappedHeight(font, longText, 200f);

GetWidestLine

Gets the width of the widest line in text.

float widest = TextHelper.GetWidestLine(font, multilineText);

CountLines

Counts the number of lines in text.

int lines = TextHelper.CountLines(multilineText);

SplitLines

Splits text into lines at newline characters.

string[] lines = TextHelper.SplitLines(multilineText);

Time Formatting

FormatTime

Formats time in seconds to M:SS format.

string time = TextHelper.FormatTime(125.5f);   // "2:05"
string time2 = TextHelper.FormatTime(65f);     // "1:05"
string time3 = TextHelper.FormatTime(-30f);    // "-0:30"

FormatTimeLong

Formats time in seconds to H:MM:SS format.

string time = TextHelper.FormatTimeLong(3665f);   // "1:01:05"
string time2 = TextHelper.FormatTimeLong(90f);    // "0:01:30"

Number Padding

PadNumber

Pads a number with leading zeros.

string padded = TextHelper.PadNumber(5, 3);   // "005"
string padded2 = TextHelper.PadNumber(42, 4); // "0042"

Cache Management

ClearCaches

Clears all caches. Call this when fonts are unloaded or changed.

TextHelper.ClearCaches();

Examples

UI Text Rendering

public void DrawDialogueBox(string text)
{
    // Wrap text to fit within the dialogue box
    string wrapped = TextHelper.WrapText(
        _font,
        text,
        _dialogueBox.Width - 20f // Padding
    );
    
    // Draw wrapped text
    _batcher.DrawText(_font, wrapped, _dialogueBox.Position + new Vect2(10f, 10f), Color.White);
}

Tooltip with Truncation

public void DrawTooltip(string text, Vect2 position, float maxWidth)
{
    // Truncate if too long
    string displayText = TextHelper.TruncateWithEllipsis(_font, text, maxWidth);
    
    // Draw tooltip
    _batcher.DrawText(_font, displayText, position, Color.White);
}

File Path Display

public void DrawFilePath(string fullPath, float maxWidth)
{
    // Show middle-ellipsized path
    string displayPath = TextHelper.EllipsizeMiddle(_font, fullPath, maxWidth);
    _batcher.DrawText(_font, displayPath, position, Color.White);
}

Timer Display

public void DrawTimer(float seconds)
{
    string timeText = TextHelper.FormatTime(seconds);
    _batcher.DrawText(_font, timeText, position, Color.White);
}

public void DrawLongTimer(float seconds)
{
    string timeText = TextHelper.FormatTimeLong(seconds);
    _batcher.DrawText(_font, timeText, position, Color.White);
}

Multi-Line Text with Indentation

public void DrawDescription(string text, Rect2 bounds)
{
    string wrapped = TextHelper.WrapTextWithIndent(
        _font,
        text,
        bounds.Width,
        indentWidth: 20f
    );
    
    _batcher.DrawText(_font, wrapped, bounds.Position, Color.White);
}

Text Cleaning from User Input

public void ProcessUserInput(string input)
{
    // Clean the input before processing
    string cleaned = TextHelper.CleanText(input);
    
    // Store cleaned text
    _userInput = cleaned;
}

Summary

Method Description
WrapText Wraps text at word boundaries
WrapTextCharacter Wraps text character by character
WrapTextWithIndent Wraps text with indentation
WrapTextToLines Wraps text and returns lines
TruncateWithEllipsis Truncates with ellipsis at the end
EllipsizeMiddle Truncates with ellipsis in the middle
CleanText Cleans and normalizes text
MeasureWrappedHeight Measures height of wrapped text
GetWidestLine Gets width of the widest line
CountLines Counts number of lines
SplitLines Splits text into lines
FormatTime Formats seconds to M:SS
FormatTimeLong Formats seconds to H:MM:SS
PadNumber Pads a number with leading zeros
ClearCaches Clears cached text data

Back to Home

Clone this wiki locally