简体   繁体   中英

Fit the text in the button control on resize by changing Fontsize dynamically

I have a application in which the text of different length is set on the button every 5 seconds. How do i resize the text to fit it in the button. Also i need to resize the font on resizing the window.(my button size increases on resizing window as i have used dock property on it.

The following is the code i used to do it, but its not working very fine when the text length is 2 or less.(the text pops a bit out of the control)

public static void FitControlFont(Control control)
        {
            if (control.Text.Length == 0)
            {
                return;
            }

            try
            {
                Font currentFont = control.Font;
                Graphics graphics = control.CreateGraphics();
                SizeF newSize = graphics.MeasureString(control.Text, control.Font);
                graphics.Dispose();

                float factorX = control.Width / newSize.Width;
                float factorY = control.Height / newSize.Height;
                float factor = factorX > factorY ? factorY : factorX;
                if (control.InvokeRequired)
                {
                    control.Invoke(new MethodInvoker(delegate { control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor); }));
                }
                else
                {
                    control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor);
                }
            }
            catch (Exception ex)
            {
                if (Exceptions.IsCritical(ex))
                {
                    throw;
                }

                return;
            }
        }

You can set the button's autosize property to true, this will make it's width fit to content.

Concerning changing the font, you can handle the SizeChanged of the button ( since you set it's dock property) and determine some ratio for the font relative to the form's height :

private void button1_SizeChanged(object sender, EventArgs e)
{
     button1.Font = new Font(button1.Font.FontFamily, this.Size.Height / 10) ;
}

I improved the function a bit because in your example the control.Width and newSize.Width are integer, control.Height and newSize.Height are also integer, so when you calculate the form factors you get an integer result and cast it as float, which is not a good form factor (and can be zero crashing the newly created Font ):

        public static void FitControlFont(Control control)
        {
            if (control.Text.Length == 0)
            {
                return;
            }

            try
            {
                Font currentFont = control.Font;
                Graphics graphics = control.CreateGraphics();
                SizeF newSize = graphics.MeasureString(control.Text, control.Font);
                graphics.Dispose();

                float factorX = ((float)control.Width) / newSize.Width;
                float factorY = ((float)control.Height) / newSize.Height;
                float factor = factorX > factorY ? factorY : factorX;
                if (control.InvokeRequired)
                {
                    control.Invoke(new MethodInvoker(delegate { control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor); }));
                }
                else
                {
                    control.Font = new Font(currentFont.Name, currentFont.SizeInPoints * factor);
                }
            }
            catch (Exception ex)
            {
                if (Exceptions.IsCritical(ex))
                {
                    throw;
                }

                return;
            }
        }

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