Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: A function that will auto adjust fonts base on the control size at runtime?

Tags:

c#

.net

winforms

After spending a good amount of time search for this function:

I thought it will be nice if anyone could give me the best way to do so. Is there a function that can dynamically adjust the font size base on the size of any window form control(label/button)?

This is what I have after researching online, unfortunately these codes give a lot of exception during run time when the control re-sizes.

public void textAdjustment()
    {
        try
        {
            while (this.label.Width < System.Windows.Forms.TextRenderer.MeasureText(this.label.Text,
               new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Width)
            {
                this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size - 1.0f, this.label.Font.Style);
            }
            if (this.label.Width > System.Windows.Forms.TextRenderer.MeasureText(this.label.Text, new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Width)
            {
                this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size + 0.1f, this.tableLabel.Font.Style);
            }
            if (this.label.Height < System.Windows.Forms.TextRenderer.MeasureText(this.label.Text, new Font(this.label.Font.FontFamily, this.label.Font.Size, this.label.Font.Style)).Height)
            {
                this.label.Font = new Font(this.label.Font.FontFamily, this.label.Font.Size - 0.6f, this.label.Font.Style);
            }
        }
        catch (Exception e)
        {
            this.label.Font = Control.DefaultFont;
        }
    }

I don't think I did my way of tackling this is right, does anyone know a way that will adjust font size base even when increase and decrease control size in run time? I personally think that this post will be a very good post for others to refer to as well.

like image 414
Ryan Fung Avatar asked Oct 07 '22 03:10

Ryan Fung


1 Answers

This is very troublesome kind of code, the proper size of the font is not that well correlated to the size of the control. Nor is it a common thing to do, a UI should be readable and consistent at any window size. It is otherwise very unclear what kind of exceptions you are seeing. Obvious mistakes would be forgetting to turn the label's AutoSize property off and not making the label tall enough. Some code to play with, beware that it is only suitable for a Label. Drop one on the form before pasting this code:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        label1.AutoSize = false;
        label1.Size = new Size(100, 60);
        label1.Text = "Autosize this";
        label1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
        label1.Resize += new EventHandler(label1_Resize);
    }

    void label1_Resize(object sender, EventArgs e) {
        using (var gr = label1.CreateGraphics()) {
            Font font = label1.Font;
            for (int size = (int)(label1.Height * 72 / gr.DpiY); size >= 8; --size) {
                font = new Font(label1.Font.FontFamily, size, label1.Font.Style);
                if (TextRenderer.MeasureText(label1.Text, font).Width <= label1.ClientSize.Width) break;
            }
            label1.Font = font;
        }
    }

    protected override void OnLoad(EventArgs e) {
        label1_Resize(this, EventArgs.Empty);
        base.OnLoad(e);
    }
}

It needs improvement, the MeasureText() method should use the TextFormatFlags that the Label control uses. But this worked well enough as posted.

like image 73
Hans Passant Avatar answered Oct 12 '22 23:10

Hans Passant