简体   繁体   中英

C# Changing the Size of a button text

I want that the Size of the Button font changes dynamically, when I change the size of the Button. So far I have placed the button on the right Location and the Size of the Button changes, when I resize the Form. But When the Button becomes to small for the text in the Button the letters just 'fall' out.

How can I change the Size of the Buttons text depending on the Button size itself?

To make the text in the button responsive use the following code:

    //paint event from button:
    private void button1_Paint(object sender, PaintEventArgs e)
    {
        float fontSize = NewFontSize(e.Graphics, button1.Size, button1.Font, button1.Text);

        // set font with Font Class and the returned Size from NewFontSize();
        Font f = new Font("Arial", fontSize, FontStyle.Bold);
        button1.Font = f;
    }

    // method to calculate the size for the font:
    public static float NewFontSize(Graphics graphics, Size size, Font font, string str)
    {
        SizeF stringSize = graphics.MeasureString(str, font);
        float wRatio = size.Width / stringSize.Width;
        float hRatio = size.Height / stringSize.Height;
        float ratio = Math.Min(hRatio, wRatio);
        return font.Size * ratio;
    }

Example of code in action:

代码实例

As you see font will be resizeable inside the button. And the text will not be thrown out of the button. You also can use this for other Controllers as well.

Since you didn't provide details in regards to font, size, and what not, I will do a general snippet just to point you in the right direction. Please modify its parameters according to your needs.

Button button = (Button)sender;
button.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

Or even something simpler

button.Font = new Font("Microsoft Sans Serif", 10);

You want to check how much height and width you have for the button first.

Then code a loop on 1pt font to something like 200

in each loop call TextRenderer.MeasureText(button.text, button.font); pass the same font but with the point size from the loop.

Then verify if the height and width from the MeasureText fits in the button limits you have given yourself. If it fits try the next higher size. If it doesn't fit, the best font size if the previous one.

For dynamic changing, I would try something like this. But it's only solve dynamic changing. For checking the text width, you need implement some more functionality to this code.

    public class MyButton : Button
{
    public MyButton()
    {

    }

    public override bool AutoSize {
        get {
            return false;
        }
        set {
            base.AutoSize = false;
        }
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);

        Font = new Font(this.Font.FontFamily,Height-10,this.Font.Style,GraphicsUnit.Pixel);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM