Perform line trimming in C#

[line trimming]

Line trimming lets you determine how drawn text ends when it reaches the end of the available space. For example, the text could stop in the middle of a word or it could stop after the last word that fits completely.

The StringFormat class controls line trimming when you use a Graphics object’s DrawString method to draw text.

The StringFormat class’s Trimming property determines what DrawString does if the string doesn’t fit inside the bounding rectangle.

The following list describes the effects of each of the possible Trimming values.

  • None: No trimming. (Seems to have the same effect as Word.)
  • Character: Stops after drawing the last character that will fit.
  • Word: Stops after drawing the last word that will fit.
  • EllipsisCharacter: Draws as many characters as possible and then ends the string with an ellipsis.
  • EllipsisWord: Draws as many words as possible and then ends the string with an ellipsis.
  • EllipsisPath: Draws the beginning of the string, an ellipsis, and then the end of the string.

The EllipsisPath value is handy when it’s useful to see the beginning and ending of the string. For example, if you are showing a file’s full path and name, then this value lets you see the beginning of the path (so you know what part of the file system holds the file) and the end of the path (so you can see the file’s name).

This example uses the following code to draw its samples.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    // A Mark Twain quote:
    const string quote =
        "The trouble ain't that there is too many fools, " +
        "but that the lightning ain't distributed right.";
    const int margin = 5;
    StringTrimming[] values =
        (StringTrimming[])Enum.GetValues(typeof(StringTrimming));
    int height =
        (ClientSize.Height - (values.Length + 1) * margin) /
            values.Length;
    int width = ClientSize.Width - 2 * margin;

    using (Font font = new Font("Times New Roman", 16))
    {
        using (StringFormat string_format = new StringFormat())
        {
            int y = margin;
            foreach (StringTrimming trimmming in values)
            {
                Rectangle rect =
                    new Rectangle(margin, y, width, height);
                e.Graphics.DrawRectangle(Pens.Black, rect);
                string_format.Trimming = trimmming;
                e.Graphics.DrawString(
                    trimmming.ToString() + ": " + quote,
                    font, Brushes.Blue, rect, string_format);

                y += height + margin;
            }
        }
    }
}

The key pieces of this code start when the program creates the StringFormat object. Notice that the code includes a using statement to automatically call that object’s Dispose method.

The code loops through the StringTrimming values returned by Enum.GetValues. For each, it sets the StringFormat object’s Trimming property and draws the string. It also draws the formatting rectangle so you can see where the text is limited.

(The full Mark Twain quote is “The trouble ain’t that there is too many fools, but that the lightning ain’t distributed right.”)


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in fonts, graphics and tagged , , , , , , , , , . Bookmark the permalink.

2 Responses to Perform line trimming in C#

  1. Pingback: Perform line clipping in C# - C# HelperC# Helper

  2. Pingback: See how much text fits in a rectangle in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *