Set font size to fit a wrapped label in C#

[font size]

The example Set font size to fit a label in C# shows how you can pick a font size to make text large but still fit inside a Label. This example is similar except it allows the text to wrap inside the Label.

The only change to the code that measures the text in the test font. The previous example used this code:

SizeF text_size = gr.MeasureString(text, test_font);

The new example uses this code:

SizeF text_size = gr.MeasureString(text, test_font, wid);

The new version specifies a maximum width in the call to MeasureString so MeasureString wraps the text if it won’t fit within the allowed width. The code then checks the size returned by MeasureString and moves to a smaller font size if the text is too tall. The text won’t be too wide because MeasureString won’t allow it to exceed the maximum width.

Download the example program or see the previous example for more details.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in drawing, fonts, graphics and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.