48

I'd like to know if there's a better approach to this problem. I want to resize a label (vertically) to accomodate certain amount of text. My label has a fixed width (about 60 chars wide before it must wrap), about 495 pixels. The font is also a fixed size (12points afaik), but the text is not.

What I want to do is increase the Label Height when there's a "NewLine" or the text must wrap; the idea is that the text is fully visible in the label. The AutoSize doesn't work because it will grow in width, not in height.

Of course I could count the number of NewLines and add: Newlines * LineHeight, and then -given that I manage to put 60 chars per line, just divide the number of chars and add as many LineHeight pixels as needed.

I was wondering if there was a more professional way to do it. Is my approach too "lame" ?

Thanks in advance.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144

12 Answers12

102

How about Graphics.MeasureString, with the overload that accepts a string, the font, and the max width? This returns a SizeF, so you can round round-off the Height.

        using(Graphics g = CreateGraphics()) {
            SizeF size = g.MeasureString(text, lbl.Font, 495);
            lbl.Height = (int) Math.Ceiling(size.Height);
            lbl.Text = text;
        }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 from me, too, but c'mon! Math.Ceiling? Mouse, meet howitzer. :) – MusiGenesis Dec 23 '08 at 14:40
  • @MusiGenesis - it ensures we round up and don't lose any pixels. Regular int cast rounds down, but yes: we could just add one to be safe... – Marc Gravell Dec 23 '08 at 14:45
  • 5
    I know this is very old, and I know that I'm nit-picking, but this thread http://stackoverflow.com/questions/6704923/textrenderer-measuretext-and-graphics-measurestring-mismatch-in-size seems to indicate that under normal circumstances TextRenderer.MeasureText gives a more accurate result than Graphics.MeasureString. – RenniePet Jun 03 '14 at 14:26
  • 5
    @RenniePet Even where your question is now `very old for me`: Your solution is even better then this answer. A short line `Size size = TextRenderer.MeasureText(text, font);` and thats it. Thanks ! – C4d May 25 '16 at 12:58
33

System.Drawing.Graphics has a MeasureString method that you can use for this purpose. Use the overload that takes a string, a font, and an int "width" parameter; this last parameter specifies the maximum width allowed for the string - use the set width of your label for this parameter.

MeasureString returns a SizeF object. Use the Height property of this returned object to set the height of your label.

Note: to get a Graphics object for this purpose, you can call this.CreateGraphics.

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
9

Graphics.MeasureString() will probably help you.

This is also one of the only usecases for using the Control.CreateGraphics() call!

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
  • What is another use case for CreateGraphics? I can't think of any other. – MusiGenesis Dec 23 '08 at 14:28
  • I said "one of the only" just in case there is another but I think it is the only one. To be honest i'm only parroting Bob Powell's GDI FAQ. – Quibblesome Dec 23 '08 at 15:22
  • The only other one I can think of is for creating custom graphics that go away when you drag another window over them, but that's more of a mis-use case. :) – MusiGenesis Dec 23 '08 at 16:12
  • I can think of one other use, sort of: I wrote a double-buffered Canvas usercontrol that overrides the CreateGraphics method to return a Graphics object from the double buffer instead of the control itself. Not sure this counts, since it's an override. – MusiGenesis Dec 23 '08 at 16:16
  • Wait.. we can make a game that relies on the properties of your mis-use case. Like wack-a-mole but where you use windows to kill the moles! We have a second use case! :D – Quibblesome Dec 23 '08 at 16:21
7
Size maxSize = new Size(495, int.MaxValue);
_label.Height = TextRenderer.MeasureText(_label.Text , _label.Font, maxSize).Height;
Hacko
  • 1,551
  • 1
  • 15
  • 11
  • 1
    label.Height = TextRenderer.MeasureText(_label.Text , _label.Font, maxSize, TextFormatFlags.WordBreak).Height; Include "WordBreak" to wrap text and have an expected height. – Olivier de Rivoyre Mar 15 '16 at 13:08
2

This "answer" is for future reference and to combat the initial assumption that AutoSize = true implies that it (a WinForms label) will never grow in height.

The following link shows the various effects of AutoSize = true with other properties such as MaximumSize. Depending upon the expected use of the question it may be appropriate to follow one of these approaches.

http://blogs.msdn.com/jfoscoding/articles/478299.aspx

  • 1
    Cool. Now I know why all the code I ever wrote to resize labels like this never worked quite right. I'd like to go back in time and slap myself, but I'd have trouble picking just one date to go back to. – MusiGenesis Dec 16 '09 at 04:01
2

I would like to propose an alternative since it's a label we are talking about and not just text rendering: GetPreferredSize. I tried Mark's answer and the problem is that it almost works: the label has some "padding" around the text and this needs to be taken into account in the width value of MeasureString. I couldn't find any apparent way to get this padding. While digging, I found this answer where the poster suggests to set the FlatStyle to System. That works, but unfortunately breaks the TextAlign which I wanted it to be MiddleLeft.

I poked into the Label code and I found out the GetPreferredSize which takes into account all the weird settings (FlatStyle, UseCompatibleTextRendering etc) and can give you the correct result for each case. So instead of g.MeasureString(text, lbl.Font, 495); you can do instead

lbl.GetPreferredSize(new Size(495, 0));

or even like this since the label size is already known:

lbl.GetPreferredSize(new Size(lbl.Width, 0));

In case you are wondering, 0 and 1 will be treated as int.MaxValue.

I don't know when GetPreferredSize was introduced, so back in 2008 when Mark wrote his answer, the above might not have been relevant. But if you still need something similar in 2021, GetPreferredSize might be a tiny bit handier -which returns a Size and not a SizeF.

Stelios Adamantidis
  • 1,866
  • 21
  • 36
1

I posted a user control which solves this problem in the accepted answer here: Autoscale Font in a TextBox Control so that its as big as possible and still fits in text area bounds

The control extends RichTextBox. It has a method: ScaleFontToFit that will automatically resize the font to make all the text fit.

Neat thing is it respects the multiline property. If it's true it allows words to wrap, Otherwise it doesn't.

Community
  • 1
  • 1
blak3r
  • 16,066
  • 16
  • 78
  • 98
0

According to this article you should use TextRenderer if you are going to use a Windows Form control for the final output. TextRenderer and Graphics.MeasureString will give different results, so use the one that matches your final mode of output.

Community
  • 1
  • 1
0

Well the 60 chars might be valid for your test text, but not all characters have the same width. For instance, compare
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
and
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

They both have 60 characters, and yet have vastly differing widths.

Yuliy
  • 17,381
  • 6
  • 41
  • 47
0

In some cases where you must use compact framework, which does not have any override methods for MeasureString(), you might consider calculating the height by yourself.

private int YukseklikAyarla(string p, Font font, int maxWidth)
    {
        int iHeight = 0;
        using (Graphics g = CreateGraphics())
        {
            var sizes = g.MeasureString(p, font); // THE ONLY METHOD WE ARE ALLOWED TO USE
            iHeight = (int)Math.Round(sizes.Height);
            var multiplier = (int)Math.Round((double)sizes.Width) / maxWidth; // DIVIDING THE TEXT WIDTH TO SEE HOW MANY LINES IT CAN HAS
            if (multiplier > 0)
            {
                iHeight = (int)(iHeight * (multiplier + 1)); // WE ADD 1 HERE BECAUSE THE TEXT ALREADY HAS A LINE
            }
        }
        return iHeight;
    }
Doğa Gençer
  • 124
  • 3
  • 15
0

Although, its an old thread, thought it might help new visitors.

In C#, you could use control.width

0

Is there any downside to using the TextRenderer class to measure the string (like in Marc's response) instead of going through the work to create a Graphics object, etc?

jean
  • 1,027
  • 1
  • 8
  • 15
  • TextRenderer is slower than Graphics.MeasureString. I have no idea why. They're both plenty fast for most use cases, however. – MusiGenesis Oct 02 '09 at 00:55