简体   繁体   English

通过动态更改字体大小来调整按钮控件中的文本

[英]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.我有一个应用程序,其中每 5 秒在按钮上设置不同长度的文本。 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.我还需要在调整 window 大小时调整字体大小。(我的按钮大小在调整 window 大小时会增加,因为我在其上使用了停靠属性。

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)以下是我用来执行此操作的代码,但当文本长度为 2 或更少时,它不能很好地工作。(文本弹出有点超出控制)

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. 您可以将按钮的autosize属性设置为true,这将使其宽度适合内容。

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 : 关于更改字体,您可以处理按钮的SizeChanged(因为您将其设置为dock属性),并确定字体相对于表单高度的比例:

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 ): 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,这不是一个好的外形(并且可以使新创建的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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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