C# - How to maximise the font size of a label that can wrap inside a container?

98 Views Asked by At

Say you have a label. This label sits inside a container, lets say a TableLayoutPanel for argument. You dont always know the amount of text that the label will hold. It can be a few words or it can be a whole paragraph. You cant resize the TableLayoutPanel cell because its already at the perfect size.

You need a Big Font size for few words but a smaller Font size for larger pieces of text.

One problem though, the text can wrap.

How would one find the largest font size that would make the text fit snugly in the provided container?

I had to deactivate the text wrapping for starters.

I then got the size of the container in pixels. With that, i have the bounding box the font must fit in.

I then got the label size.

If the label is biger than the container, i incrementally made the font smaller. If the label is smaller than the container, i incrementally made the font bigger.

I did this until the label fit inside the container. One issue though, the label can get very small if there are lots of text.

1

There are 1 best solutions below

0
On

It took ages but I managed to figure out an elegant solution (kinda) and I wanted to share my "discoveries"

For starters, I re-enabled wrapping via the AutoSize (property this is important).

Then I got the height of the container, the width is no longer important.

I set the Anchor property to Top | Left | Right, meaning that the label will sit at the top and flow downwards.

Then I made the font size waaaaay too big for the text provided - it must overflow and hide text.

A sneaky quirk of this arrangement is that the label will overflow to outside the container but the overflowed text won't be visible. The size of the label will be exactly the height of the container, despite overflowing and having text missing. This same height can be used to our advantage.

The label inside a container where the font is too big sothat it overflows

If you then make the text smaller bit by bit, there will come to a point where the label height will no longer match its original height and get smaller. At that moment, you can stop because you have the perfect font size. The label will no longer overflow and the text will comfortably sit in the container.

The font size was made smaller and it now sits perfectly

Some code:

//get the height of the tabeLayoutPanel cell that serves as the container
float maxHeightClearance = tableLayoutPanelMain.RowStyles[4].Height;

//get the height of the label and save it for comparison later
float labelHeight = labelCourseName.Size.Height;

//for as long as the label height remains unchanged, it means that it is still too big
while(labelCourseName.Size.Height == labelHeight)
{
    //slowly decrease the font size
    labelCourseName.Font = new Font(labelCourseName.Font.Name, labelCourseName.Font.Size - 0.2f, labelCourseName.Font.Style, labelCourseName.Font.Unit);

    //refresh the GUI to see if anything changed. This is nearly instantaneous
    Application.DoEvents();
}