Reputation: 139
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);
Upvotes: 1
Views: 1189
Reputation: 1
@jimi , I know its been a while since he posted the code for his custom label class. I modified a little bit his code to allow the custom label perform text aligment. This might be useful for someone else in the future
using System; 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()) { // Ajusta la alineación del texto según las propiedades de la etiqueta switch (this.TextAlign) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: format.Alignment = StringAlignment.Near; break; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: format.Alignment = StringAlignment.Center; break; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: format.Alignment = StringAlignment.Far; break; } switch (this.TextAlign) { case ContentAlignment.TopLeft: case ContentAlignment.TopCenter: case ContentAlignment.TopRight: format.LineAlignment = StringAlignment.Near; break; case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleRight: format.LineAlignment = StringAlignment.Center; break; case ContentAlignment.BottomLeft: case ContentAlignment.BottomCenter: case ContentAlignment.BottomRight: format.LineAlignment = StringAlignment.Far; break; } 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); } } }
Upvotes: 0
Reputation: 32223
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);
}
}
}
Upvotes: 1