I am using .NET to draw a string into a limited space. I want the string to be as big as possible. I have no problem in the string breaking up into more lines (if it stays inside the rectangle). Now the problem: I don't want .NET to break the string in different lines in the middle of a word. For example string "Test" prints on a single line in a big font. String "Testing" should print on a single line in a smaller font (and not "Testi" on one line and "ng" on another) and string "Test Test" should print on two lines in a fairly large font.
Anybody got ideas on how to restrict .NET not to break my words?
I'm currently using a code like this:
internal static void PaintString(string s, int x, int y, int height, int maxwidth, Graphics g, bool underline)
{
FontStyle fs = FontStyle.Bold;
if (underline)
fs |= FontStyle.Underline;
Font fnt = new System.Drawing.Font("Arial", 18, fs);
SizeF size = g.MeasureString(s, fnt, maxwidth);
while (size.Height > height)
{
fnt = new System.Drawing.Font("Arial", fnt.Size - 1, fs);
size = g.MeasureString(s, fnt, maxwidth);
}
y = (int)(y + height / 2 - size.Height / 2);
g.DrawString(s, fnt, new SolidBrush(Color.Black), new Rectangle(x, y, maxwidth, height));
}