Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<string> WrapText(string text, int maxLineWidth)
- {
- string[] split = text.Split(' ');
- List<string> words = new List<string>(split);
- List<string> results = new List<string>();
- StringBuilder sb = new StringBuilder();
- float lineWidth = 0f;
- float spaceWidth = MeasureString(" ").Width;
- for (int i = 0; i < words.Count; i++)
- {
- string word = words[i];
- float size = MeasureString(word).Width;
- if (size > maxLineWidth)
- {
- words.Remove(word);
- foreach (var result in WrapWord(word, maxLineWidth).Reverse())
- {
- word = words[i];
- }
- word = words[i];
- if (lineWidth + size < maxLineWidth)
- {
- sb.Append(word + " ");
- lineWidth += size + spaceWidth;
- }
- else
- {
- results.Add(sb.ToString());
- sb.Clear();
- sb.Append(word + " ");
- lineWidth = size + spaceWidth;
- }
- }
- }
- return results;
- }
- public IEnumerable<string> WrapWord(string word, int maxLineWidth)
- {
- for (int i = 0; i < word.Length; i++)
- {
- var sub = word.Substring(0, i);
- if (MeasureString(sub).Width < maxLineWidth)
- {
- yield return sub;
- var end = word.Substring(i);
- if (MeasureString(end).Width < maxLineWidth)
- {
- yield return end;
- }
- else
- {
- foreach (var result in WrapWord(end, maxLineWidth))
- {
- yield return result;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement