0

Using Windows forms, I have a rectangle in which I want to paint some text inside using Graphics.DrawString. What I want to do though, is create the font as large as possible, whilst keeping the text within the bounds of the rectangle and only wrapping text to the next line between words.

e.g.

Not acceptable       Acceptable

   +-------+         +--------+
   | dotne |         |        |
   |   t   |         | dotnet |
   | rocks |         |  rocks |
   +-------+         +--------+

in semi-pseudo code, this is what I'm thinking of

string text = "This is some Text";
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
RectangleF rect = new RectangleF(0,0,100,100);
float maxFontSize = 10F;
float minFontSize = 6F;
float fontSize = maxFontSize;
Font font = new Font(fontFamily, fontSize);
bool found = false;
do
{
    font = new Font(fontFamily, fontSize);
    if TextFitsWithinBounds(text, font, rect, sf)
        found = true;
    else
        fontSize -= 0.1F;
} while (found == false && fontSize > minFontSize)
Graphics.DrawString(text, font, Brushes.Black, rect, sf);

What I'm looking for is a means of implementing TextFitsWithinBounds(). Is there a class anywhere in the framework that will help me achieve this?

MeasureString comes close, but that doesn't appear to allow me to specify the bounds.

Community
  • 1
  • 1
Bryan
  • 3,224
  • 9
  • 41
  • 58
  • Referencing to my previous work again, I'll advise you from doing what you try to do. My first version of the app had EXACTLY what you are doing - self growing text on touch-screen buttons. However, it didn't work well from two angles - it had problems with word wrapping, and the looks of it weren't nice. Instead, I'm offering you to find some nice little font and format the text with the same character size always. It will look better, trust me. – Daniel Mošmondor May 24 '11 at 10:25
  • @Daniel: Thanks, I certainly haven't dismissed your idea from my previous question, quite the opposite infact, but right now, I'm just trying to knock up a quick proof of concept for a customer, and I haven't time to recreate the visual styles that Windows 7 can offer me with a button. When we get beyond POC, I'll certainly revisit, and almost certainly use the method you described. Had I have had more time, I'd have accepted your answer to my previous question, but right now it's a trade off between looks and time. – Bryan May 24 '11 at 10:43

1 Answers1

1

The graphics object has a .MeasureString() member, resulting in the size of the drawn text.

Mr47
  • 2,655
  • 1
  • 19
  • 25
  • I've tried MeasureString, but that doesn't seem to allow me to specify the size of the bounds, instead it seems to calculates the size of the bounds, without any way of me specifying the constraints I'm working too. – Bryan May 24 '11 at 10:09
  • On second thoughts, there is an overload to MeasureString that allows this. http://msdn.microsoft.com/en-us/library/957webty.aspx – Bryan May 24 '11 at 10:23
  • 1
    @Bryan: from the look of your example, you are doing 'constraining' in the outer loop. Implementing TextFitsWithinBounds should be as trivial as using MeasureString and then seeing if the resulting rectangle is larger than yours that you sent in as a parameter :) – Daniel Mošmondor May 24 '11 at 10:26