在不超过最大字体大小的情况下调整标签的字体大小以适合面板 [英] Resize a Label's font size to fit within a Panel without exceeding a maximum font size

查看:38
本文介绍了在不超过最大字体大小的情况下调整标签的字体大小以适合面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个布尔代数简化器.为此,我在简化过程中的关键阶段采用表达式,将其添加到列表中,最后使用这些表达式来展示计算机如何简化表达式.例如.对于 B•(A+~A),标签的内容将是:

B•(A+~A)//初始表达式= B•(1)//括号简化= B•1//移除括号= B//简化表达式.

我创建了一个标准面板,并将标签拖放到其中,以将标签停靠在面板内.

我想让它改变文本的字体大小,使其完全适合面板,因为一些复杂的表达式可能有很多行,因此可能不适合屏幕.

但是,如果用户输入了一个非常简单的表达式,例如A+1,答案和工作线会很小.在这种情况下,我不希望文本填充面板,因为字体会很大.

因此,我试图使标签的字体大小发生变化,使表达式适合面板,但限制最大字体大小,以便少量文本不使用大字体大小.

有人知道怎么做吗?
我在网上搜索并找到以下代码,但这并没有填充面板:

WorkingOutLabel.Font = new Font(WorkingOutLabel.Font.FontFamily,PanelHoldingWorkingLabel.Font.Height, FontStyle.Regular);

解决方案

从 Label 派生的自定义控件,可缩放其文本大小以适合控件的边界.

使用 System.ComponentModel;使用 System.Drawing;使用 System.Drawing.Text;使用 System.Windows.Forms;[设计师类别(代码")]类 AutoScaleLabel : 标签{公共 AutoScaleLabel() =>初始化组件();私有无效InitializeComponent(){this.SetStyle(ControlStyles.AllPaintingInWmPaint |ControlStyles.UserPaint |ControlStyles.ResizeRedraw, true);this.UpdateStyles();}protected override void OnLayout(LayoutEventArgs e){base.OnLayout(e);this.AutoSize = false;}protected override void OnPaint(PaintEventArgs e){使用 (SolidBrush Brush = new SolidBrush(this.ForeColor))使用 (StringFormat format = new StringFormat(StringFormatFlags.NoClip |StringFormatFlags.NoWrap |StringFormatFlags.FitBlackBox)){format.Trimming = StringTrimming.None;SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font, this.ClientSize, format);if (textSize.Width > this.ClientSize.Width){浮动比例=(浮动)this.ClientSize.Width/textSize.Width;e.Graphics.ScaleTransform(scale, scale);}e.Graphics.Clear(this.BackColor);e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;e.Graphics.DrawString(this.Text, this.Font, Brush, this.ClientRectangle, format);}}}

I have created a boolean algebraic simplifier. For this, I take the expression at key stages during the simplification process, add it to a list and at the end use these expressions to show how the computer simplified the expression. E.g. For B•(A+~A) the contents of the label will be:

B•(A+~A) // Initial Expression 
= B•(1)  // Brackets simplified 
= B•1    // Brackets removed 
= B      // Simplified expression.

I have created a standard Panel and I have dragged and dropped the Label within it, to dock the Label within the Panel.

I want to make it so that the font size of the text changes so that it fits the panel fully, because some complicated expressions may have lots of lines of working and may therefore otherwise not fit the screen.

However, if the user inputs a really simple expression e.g. A+1, the answer and the lines of working will be small. In this case I do not want the text to fill the panel as the font would be huge.

I am therefore trying to make it so that the Label's font size changes to make the expression fit within the Panel but limit the maximum font size so that a small amount of text does not use a massive font size.

Does anyone know how to do this?
I searched online and found the following code, however this does not fill the Panel:

WorkingOutLabel.Font = new Font(WorkingOutLabel.Font.FontFamily, 
    PanelHoldingWorkingLabel.Font.Height, FontStyle.Regular);

解决方案

A Custom Control derived from Label that scales its Text size to fit the control's bounds.
A Graphics.ScaleTransform() transformation is applied when the calculated width of the Text is larger that the Control's ClientArea.
The Text is scaled when the controls is resized and/or when the Text changes.

Sample functionality:

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;

[DesignerCategory("Code")]
class AutoScaleLabel : Label
{
    public AutoScaleLabel() => InitializeComponent();

    private void InitializeComponent()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                      ControlStyles.UserPaint |
                      ControlStyles.ResizeRedraw, true);
        this.UpdateStyles();
    }

    protected override void OnLayout(LayoutEventArgs e)
    {
        base.OnLayout(e);
        this.AutoSize = false;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (SolidBrush brush = new SolidBrush(this.ForeColor))
        using (StringFormat format = new StringFormat(StringFormatFlags.NoClip | 
               StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox))
        {
            format.Trimming = StringTrimming.None;
            SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font, this.ClientSize, format);
            if (textSize.Width > this.ClientSize.Width)
            {
                float scale = (float)this.ClientSize.Width / textSize.Width;
                e.Graphics.ScaleTransform(scale, scale);
            }
            e.Graphics.Clear(this.BackColor);
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            e.Graphics.DrawString(this.Text, this.Font, brush, this.ClientRectangle, format);
        }
    }
}

这篇关于在不超过最大字体大小的情况下调整标签的字体大小以适合面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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