-
Notifications
You must be signed in to change notification settings - Fork 1
TextHelper
TextHelper provides comprehensive text manipulation utilities including wrapping, truncation, measurement, and formatting for UI and rendering systems.
| 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 |
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);Wraps text character by character to fit within a specified width.
string wrapped = TextHelper.WrapTextCharacter(font, longText, 200f);Wraps text with a specified indentation for subsequent lines.
string indented = TextHelper.WrapTextWithIndent(
font,
longText,
maxWidth: 200f,
indentWidth: 20f
);Wraps text and returns an array of lines.
string[] lines = TextHelper.WrapTextToLines(font, longText, 200f);
foreach (var line in lines)
{
// Process each line
}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..."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"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"Measures the height of text after wrapping to a specified width.
float height = TextHelper.MeasureWrappedHeight(font, longText, 200f);Gets the width of the widest line in text.
float widest = TextHelper.GetWidestLine(font, multilineText);Counts the number of lines in text.
int lines = TextHelper.CountLines(multilineText);Splits text into lines at newline characters.
string[] lines = TextHelper.SplitLines(multilineText);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"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"Pads a number with leading zeros.
string padded = TextHelper.PadNumber(5, 3); // "005"
string padded2 = TextHelper.PadNumber(42, 4); // "0042"Clears all caches. Call this when fonts are unloaded or changed.
TextHelper.ClearCaches();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);
}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);
}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);
}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);
}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);
}public void ProcessUserInput(string input)
{
// Clean the input before processing
string cleaned = TextHelper.CleanText(input);
// Store cleaned text
_userInput = cleaned;
}| 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 |
- 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