Wednesday, October 20, 2010

Drawing Text with Ellipsis (.NET C#)

Although I have not used it extensively, the StringFormat.Trimming functionality could come in handy in many situations. This will change the way Graphics.DrawString renders text within the destination rectangle. You can specify a visual indicator to the user that the text contents are larger than the rectangle it is being drawn within. Based on the StringFormat.Trimming setting the behavior will differ.

StringFormat stringFormat = new StringFormat();
stringFormat.Trimming = StringTrimming.Character;
stringFormat.Trimming = StringTrimming.EllipsisCharacter;
stringFormat.Trimming = StringTrimming.EllipsisPath;
stringFormat.Trimming = StringTrimming.EllipsisWord;
stringFormat.Trimming = StringTrimming.None;
stringFormat.Trimming = StringTrimming.Word;
(Note: The above is just a list of possible settings... please do not write code like this ever)
Unfortunately there is no indication, other than the obvious visual, that a string was truncated. I am still investigating solutions to determine if the text will be cut off by the destination rectangle. Graphics.MeasureString is far from fast and Graphics.MeasureCharacterRanges seems to be a bit of a mystery thus far. This will likely become a later topic unless I just forget about it...

No comments:

Post a Comment