Sulla base del article fornito da @brgerner, fornirò l'implementazione alternativa qui, come quello contrassegnato come risposta non è così efficiente né completo come questo qui sotto:
public class FontWizard
{
public static Font FlexFont(Graphics g, float minFontSize, float maxFontSize, Size layoutSize, string s, Font f, out SizeF extent)
{
if (maxFontSize == minFontSize)
f = new Font(f.FontFamily, minFontSize, f.Style);
extent = g.MeasureString(s, f);
if (maxFontSize <= minFontSize)
return f;
float hRatio = layoutSize.Height/extent.Height;
float wRatio = layoutSize.Width/extent.Width;
float ratio = (hRatio < wRatio) ? hRatio : wRatio;
float newSize = f.Size * ratio;
if (newSize < minFontSize)
newSize = minFontSize;
else if (newSize > maxFontSize)
newSize = maxFontSize;
f = new Font(f.FontFamily, newSize, f.Style);
extent = g.MeasureString(s, f);
return f;
}
public static void OnPaint(object sender, PaintEventArgs e, string text)
{
var control = sender as Control;
if (control == null)
return;
control.Text = string.Empty; //delete old stuff
var rectangle = control.ClientRectangle;
using (Font f = new System.Drawing.Font("Microsoft Sans Serif", 20.25f, FontStyle.Bold))
{
SizeF size;
using (Font f2 = FontWizard.FlexFont(e.Graphics, 5, 50, rectangle.Size, text, f, out size))
{
PointF p = new PointF((rectangle.Width - size.Width)/2, (rectangle.Height - size.Height)/2);
e.Graphics.DrawString(text, f2, Brushes.Black, p);
}
}
}
}
e l'utilizzo:
val label = new Label();
label.Paint += (sender, e) => FontWizard.OnPaint(sender, e, text);
Cosa vi aspettate che accada se il testo diventa a lungo da visualizzare sul modulo? –
Imposta il tag per framework GUI, WinForms o asp.net o altro. – brgerner
@ M.Babcock mi aspetto che diventi più piccolo in modo che possa adattarsi all'etichetta –