views:

261

answers:

2

Hello,

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));
    }
A: 

You could resize the control size depending upon the length/size of the string. This would make sure that the string fits in one line.

Aseem Gautam
But I have a fixed space which I don't want to resize. I want to scale down/wrap the text so that it fits
JanHudecek
A: 

What you have got there seems to be the right answer. I dont think there is a single call to the framework method that can do all that for you. Another option if you are rending button and text in winform, you should look at the ButtonRenderer and TextRenderer class. When calling DrawText or MeasureString you can also specify TextFormatFlags which will allow you to specify WorkBreak, SingleLine or using Ellipse truncation.

Fadrian Sudaman