public Font FindMatch(string text, FontMatchFormat format) { var proposedSize = format.ProposedSize; var size = format.Size; var minSize = format.MinSize; while (true) { if (size <= 1.0F) { return(this[1.0F, format.Style]); } var font = this[size, format.Style]; var textSize = TextRenderer.MeasureText(text, font, proposedSize, format.Flags); if (textSize.Height <= proposedSize.Height && textSize.Width <= proposedSize.Width) { return(font); } else if (size > minSize) { size--; } else { return(font); } } }
public static void FitFontSize(this Control control, float max, string text) { var format = new FontMatchFormat { Size = max, ProposedSize = control.Size }; control.Font = OptimizedControl.DefaultFontManager.FindMatch(text, format); }