public static String BreakAndCenterStringAccoringToLineLength(String input, float maxLineLength, Microsoft.Xna.Framework.Graphics.SpriteFont font) { var words = input.Split(' '); var lines = new List <String>(); var spaceLen = font.MeasureString(" ").X; var currentLineLength = 0f; StringBuilder currentLine = new StringBuilder(); foreach (var word in words) { var wordLen = font.MeasureString(word).X; if (currentLineLength > 0) { if ((currentLineLength + spaceLen + wordLen) > maxLineLength) { lines.Add(AddSpacesAccordingToLength(currentLine.ToString(), spaceLen, maxLineLength - currentLineLength)); currentLine.Clear(); currentLine.Append(word); currentLineLength = wordLen; } else { currentLine.Append(' ').Append(word); currentLineLength += wordLen + spaceLen; } } else { currentLine.Append(word); currentLineLength += wordLen; } } lines.Add(AddSpacesAccordingToLength(currentLine.ToString(), spaceLen, maxLineLength - currentLineLength)); return(String.Join("\n", lines)); }
public Vector2 Measure(string input) { return(_font.MeasureString(input)); }
public override Vector2 MeasureString(string text) { XnaVector2 vector = spriteFont.MeasureString(text); return(new Vector2(vector.X, vector.Y)); }