I use .NET to draw a string in a limited space. I want the string to be as large as possible. I have no problem in that the line is split into several lines (if it remains inside the rectangle). Now the problem is: I do not want .NET to split the string into different lines in the middle of the word. For example, the line "Test" prints on one line in large print. The line "Test" should print on one line in a smaller font (not "Testi" on one line and "ng" on the other), and the line "Test Test" should print on two lines in a rather large font.
Has anyone got ideas on how to restrict .NET not to break my words?
I am currently using the following code:
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));
}