Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jackmott
Last active November 11, 2018 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackmott/e752ba2f8471ad3d7f1b6c82c06233bb to your computer and use it in GitHub Desktop.
Save jackmott/e752ba2f8471ad3d7f1b6c82c06233bb to your computer and use it in GitHub Desktop.
word wrap for monogame
public static string Wrap(string s, int width, Func<string, int> widthMeasure)
{
StringBuilder builder = new StringBuilder(s.Length);
var text = s.AsSpan();
int start = 0;
int len = 0;
for (int i = 0; i < text.Length; i++) {
if (text[i] == ' ')
{
if (widthMeasure(text.Slice(start, i-start).ToString()) <= width)
{
len = i - start;
}
else
{
builder.AppendLine(text.Slice(start,len).ToString());
start = i;
len = 0;
}
}
}
builder.AppendLine(text.Slice(start).ToString());
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment