示例#1
0
 /// <summary>
 /// Splits the text into lines to fit inside the specified width.
 /// </summary>
 /// <param name="text">The text to be splitted.</param>
 /// <param name="font">The font to use for the width calculation.</param>
 /// <returns></returns>
 List<string> SplitAndRejoin(string text, SdlDotNet.Graphics.Font font)
 {
     List<string> retLines = new List<string>();
     string[] lines = text.Split('\n');
     foreach (string line in lines) {
         string[] words = line.Trim().Split(' ');
         int i = 0;
         List<string> currentLine = new List<string>();
         while (i < words.Length) {
             currentLine.Add(words[i]);
             if (currentLine.Count != 1 && font.SizeText(string.Join(" ", currentLine.ToArray())).Width > width - 100) {
                 currentLine.RemoveAt(currentLine.Count - 1);
                 retLines.Add(string.Join(" ", currentLine.ToArray()));
                 currentLine = new List<string>();
             }
             else {
                 ++i;
             }
         }
         if (currentLine.Count != 0) {
             retLines.Add(string.Join(" ", currentLine.ToArray()));
             currentLine = new List<string>();
         }
     }
     return retLines;
 }