Resize font so text fills entire label

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I have a label control that will be filled with text at runtime. The
length of the text will vary, but the label must stay the same size. Is
there a way to set the font size of the text such that it completely
fills the label?

For example, if the text is "Santa Claus" the font size might have to be
130 points to fill up a 637x476 label, where if the text is "Once upon a
midnight dreary / while I pondered weak and weary / over many a quaint
and curious volume of forgotten lore" the font size might have to be 40
points to fill up the 637x476 label.

I've seen a lot online about sizing a control to match text length, but
nothing about sizing text to match a control. BTW, I don't have to use a
label control - if another control will help accomplish my task, great.

Thanks,

Joe
 
This is an oft' asked question.

There is no simple solution. The way to go is to use MeasureString with
different font sizes and see what sort of rectangle it returns.

An adaptation of the binary search can be used to select a rectangle of the
correct size by changing the font size.

http://en.wikipedia.org/wiki/Binary_search

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Joe said:
I have a label control that will be filled with text at runtime. The length
of the text will vary, but the label must stay the same size. Is there a
way to set the font size of the text such that it completely fills the
label?

For example, if the text is "Santa Claus" the font size might have to be
130 points to fill up a 637x476 label, where if the text is "Once upon a
midnight dreary / while I pondered weak and weary / over many a quaint and
curious volume of forgotten lore" the font size might have to be 40 points
to fill up the 637x476 label.

I've seen a lot online about sizing a control to match text length, but
nothing about sizing text to match a control. BTW, I don't have to use a
label control - if another control will help accomplish my task, great.


Put a label and a textbox on a Form. This works quite well:

Private Sub TextBox1_TextChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged

Dim f As Font
Dim g As Graphics
Dim s As SizeF
Dim Faktor, FaktorX, FaktorY As Single

If TextBox1.Text.Length = 0 Then Return

g = Me.Label1.CreateGraphics
s = g.MeasureString(TextBox1.Text, Label1.Font)
g.Dispose()

FaktorX = Label1.Width / s.Width
FaktorY = Label1.Height / s.Height

If FaktorX > FaktorY Then
Faktor = FaktorY
Else
Faktor = FaktorX
End If

f = Label1.Font
Label1.Font = New Font(f.Name, f.SizeInPoints * Faktor)

Me.Text = Label1.Font.SizeInPoints.ToString
Label1.Text = TextBox1.Text

End Sub


Type into the textbox and the code fits the text into the label.


Armin
 
Thanks Armin. This works perfectly for a single line of text - I'll
certainly hang on to it. Unfortunately it doesn't take word wrapping
into consideration, so it won't work for my current application.

Joe
 
Joe said:
Thanks Armin. This works perfectly for a single line of text - I'll
certainly hang on to it. Unfortunately it doesn't take word wrapping
into consideration, so it won't work for my current application.


I tried it with a multiline textbox and it works perfectly, too.


Armin
 
The code you posted will work if multiple lines (ending with carriage
returns) are entered in the textbox, but not with text that's wrapped
automatically without carriage returns.

I was able to resolve that with a minor change from:

s = g.MeasureString(TextBox1.Text, Label1.Font)

to

s = g.MeasureString(TextBox1.Text, Label1.Font, Label1.Width)


Thanks again!

Joe
 
Joe said:
The code you posted will work if multiple lines (ending with
carriage returns) are entered in the textbox, but not with text
that's wrapped automatically without carriage returns.

Which wrapped text? In a string, there is no wrapped text, and in the label,
you don't want to have wrapped text.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top