public static List WordWrapText()

in Configurator/Base/Classes/ExtensionMethods.cs [1350:1404]


    public static List<string> WordWrapText(this Control control, string proposedText, int overridingWidth = 0)
    {
      if (control == null)
      {
        return null;
      }

      List<string> wordWrapLines = new List<string>();
      if (control.AutoSize)
      {
        wordWrapLines.Add(proposedText);
        return wordWrapLines;
      }

      if (overridingWidth <= 0)
      {
        overridingWidth = control.Width;
      }

      string remainingText = proposedText.Trim();
      do
      {
        SizeF stringSize = TextRenderer.MeasureText(remainingText, control.Font);
        double trimPercentage = overridingWidth / stringSize.Width;
        string textToDraw;
        if (trimPercentage < 1)
        {
          int lengthToCut = Convert.ToInt32(remainingText.Length * trimPercentage);
          lengthToCut = lengthToCut > 0 ? lengthToCut - 1 : 0;
          int spaceBeforePos = lengthToCut;
          int spaceAfterPos = remainingText.IndexOf(" ", lengthToCut, StringComparison.Ordinal);
          textToDraw = spaceAfterPos >= 0 ? remainingText.Substring(0, spaceAfterPos) : remainingText;
          while (spaceBeforePos > -1 && TextRenderer.MeasureText(textToDraw, control.Font).Width > overridingWidth)
          {
            spaceBeforePos = remainingText.LastIndexOf(" ", spaceBeforePos, StringComparison.Ordinal);
            textToDraw = spaceBeforePos >= 0 ? remainingText.Substring(0, spaceBeforePos) : textToDraw;
            spaceBeforePos--;
          }
        }
        else
        {
          textToDraw = remainingText;
        }

        textToDraw = textToDraw.Trim();
        if (textToDraw.Length > 0)
        {
          wordWrapLines.Add(textToDraw);
        }

        remainingText = textToDraw.Length < remainingText.Length ? remainingText.Substring(textToDraw.Length).Trim() : string.Empty;
      } while (remainingText.Length > 0);

      return wordWrapLines;
    }