Graphics.DrawString chopping mid-word
I have created a usercontrol which is basically a button w开发者_运维知识库ith some nice little features.
One of those features is that it determines the largest font size possible for the text, whilst keeping the text within the confines on the box.
This works fine in the majority of cases, but sometimes it will chop a word in half to fit it in.
So it might appear as..
stackov
erflow
Rather than
stackoverflow
(but in a smaller font)
I thought that there would have been a StringFormatFlag to allow me to specify how word-wrapping is done.
I want word wrapping, but not 'character' wrapping.
Thanks Rich.
You could try using TextRenderer instead:
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);
You need to specify StringFormatFlags.NoWrap when populating the StringFormat.FormatFlags property. Then use one of the DrawString overloads that accepts a StringFormat class. One of the following three methods:
DrawString(String, Font, Brush, PointF, StringFormat)
DrawString(String, Font, Brush, RectangleF, StringFormat)
DrawString(String, Font, Brush, Single, Single, StringFormat)
精彩评论