Graphics.DrawString shredding the middle of a word

I created a usercontrol, which basically is a button with some small features.

One of these functions is that it determines the maximum font size available for the text, while preserving the text inside the borders.

This works great in most cases, but sometimes it will cut the word in half to fit it.

So it might look like ..

stackov erflow 

Instead

 stackoverflow 

(but in a smaller font)

I thought there would be a StringFormatFlag so that I can specify how word wrap is done.

I want word wrap, but not "symbolic" packaging.

Thanks Rich.

+4
source share
2 answers

Instead, you can try using TextRenderer:

  TextRenderer.DrawText(e.Graphics, "stackoverflow", this.Font, new Rectangle(10, 10, 32, 32), Color.Black, Color.Empty, TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); TextRenderer.DrawText(e.Graphics, "stack overflow", this.Font, new Rectangle(50, 10, 32, 32), Color.Black, Color.Empty, TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); 

enter image description here

+3
source

You need to specify StringFormatFlags .NoWrap when populating StringFormat.FormatFlags . Then use one of the DrawString overloads, which takes the StringFormat class. One of the following three ways:

DrawString (String, Font, Brush, PointF, StringFormat)

DrawString (String, Font, Brush, RectangleF, StringFormat)

DrawString (String, Font, Brush, Single, Single, StringFormat)

-1
source

All Articles