简体   繁体   English

C#:一个在运行时根据控件大小自动调整字体的函数?

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

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. 我个人认为这篇文章对于其他人来说也是一个非常好的帖子。

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. 这也不是常见的事情,UI在任何窗口大小都应该是可读的和一致的。 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. 明显的错误是忘记关闭标签的AutoSize属性而不是使标签足够高。 Some code to play with, beware that it is only suitable for a Label. 有些代码可以使用,请注意它只适用于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. 它需要改进,MeasureText()方法应该使用Label控件使用的TextFormatFlags。 But this worked well enough as posted. 但是这个发布得很好。

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

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