自动调整标签字体大小 [英] AutoFit Label Font Size

查看:226
本文介绍了自动调整标签字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 System.Windows.Forms.Label 是有办法来自动调整取决于标签尺寸标签字体大小?

解决方案

 类AutoFontLabel:标签
{
    公共AutoFontLabel()
        : 基础()
    {
        this.AutoEllipsis = TRUE;
    }

    保护覆盖无效OnPaddingChanged(EventArgs的发送)
    {
        UpdateFontSize();
        base.OnPaddingChanged(E);
    }

    保护覆盖无效onResize受到(EventArgs的发送)
    {
        UpdateFontSize();
        base.OnResize(E);
    }

    私人无效UpdateFontSize()
    {
        INT textHeight不同= this.ClientRectangle.Height
             -  this.Padding.Top  -  this.Padding.Bottom;

        如果(textHeight不同大于0)
        {
            this.Font =新的字体(this.Font.FontFamily,
                textHeight不同,GraphicsUnit.Pixel);
        }
    }
}
 

由于AMissico的更新控制处理填充。我们可以看到如何改变填充和textAlign设置的affectd的设计师。

For a System.Windows.Forms.Label is there a way to auto-fit the label font size depending on the label size?

解决方案

class AutoFontLabel : Label
{
    public AutoFontLabel()
        : base()
    {
        this.AutoEllipsis = true;
    }

    protected override void OnPaddingChanged(EventArgs e)
    {
        UpdateFontSize();
        base.OnPaddingChanged(e);
    }

    protected override void OnResize(EventArgs e)
    {
        UpdateFontSize();
        base.OnResize(e);
    }

    private void UpdateFontSize()
    {
        int textHeight = this.ClientRectangle.Height
            - this.Padding.Top - this.Padding.Bottom;

        if (textHeight > 0)
        {
            this.Font = new Font(this.Font.FontFamily,
                textHeight, GraphicsUnit.Pixel);
        }
    }
}

Thanks to AMissico that updated the control to handle padding. We can see how changing the Padding and TextAlign are affectd in the designer.

这篇关于自动调整标签字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆