Having spent a lot of time searching for this function:
I thought it would be nice if anyone could give me a better way to do this. Is there a function that can dynamically adjust the font size base to the size of any window shape control (label / button)?
This is what I got after studying online, but unfortunately these codes lead to a lot of runtime exceptions when management changes change.
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;
}
}
, , , - , , ?
, , .