blah blah blah is here! blah blah » Close

up0down
link

Hi!
Does any know how to find the best font size, to fit on a rectangle ?
I try using rectangle like this:
float x = 2;
float y = 80;
float width = this.Size.Width - 11;
float height = this.Size.Height - 11;
drawRect = new RectangleF(x, y, width, height);
String drawString = _text;
Font drawFont = new Font("Arial", 40);
SolidBrush drawBrush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(this.ForeColor);
e.Graphics.DrawRectangle(blackPen, x, y, width, height);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
I need the best fit font!
Thanks David

last answered 2 years ago

1 answers

up0down
link

I don't think there's a .net ready way to determine the best font size to fit a string in a fixed region. Only the other way around where you find the region the string uses at a particular font.
I suppose you can calculate the dimensions if you know the conversion factors between pixels and points. Or you could try some guess and checking. Using Graphics.<a target="_new" href="http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx">MeasureString</a>() to get its size and see if it is smaller than the desired region. Do so until you are satisfied.

up0down
link

You are right Jeff, i found this on internet and works:
float width = this.Size.Width - 11;
float height = this.Size.Height - 250;
float x = 0;
float y = 0;
RectangleF drawRect = new RectangleF(x, y, width, height);
Font font = new Font("Arial Ce", 1000);
SolidBrush textBrush = new SolidBrush(Color.Red);
int nCharsFitted; //this is how we know we have all of the characters on the canvas
int nLinesFilled; //this tells us how many lines the text uses
//create new canvas and set the size
SizeF canvasSize = drawRect.Size;
SizeF canvasSizeF = new SizeF(drawRect.Width, drawRect.Height);
//specify formatting options
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.FitBlackBox & StringFormatFlags.NoClip;
stringFormat.Alignment = StringAlignment.Center;
SizeF textSizeF = new SizeF(0, 0);
while (true)
{
textSizeF = e.Graphics.MeasureString(_text, font, canvasSizeF, stringFormat, out nCharsFitted, out nLinesFilled);
if ((canvasSizeF.Height >= textSizeF.Height) & (canvasSizeF.Width >= textSizeF.Width) & (nCharsFitted == _text.Length) &
(nLinesFilled * font.Height <= canvasSizeF.Height))
{
break;
}
else
{
font = new Font(font.FontFamily, font.SizeInPoints - 1, font.Style);
}
}
PointF textPoint;
textPoint = new PointF((canvasSizeF.Width - textSizeF.Width) / 2, 0);
e.Graphics.DrawString(_text, font, textBrush, new RectangleF(textPoint, canvasSizeF), stringFormat);
textBrush = null;
e = null;
stringFormat = null;
David

up0down
link

don't use the graphics measure string, use TextRenderer.MeasureText

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback