473,418 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Resize font so text fills entire label

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
Dec 3 '05 #1
7 19499
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" <jh@thezac.cannedluncheonmeatobfuscator.com> wrote in message
news:uv****************@TK2MSFTNGP11.phx.gbl...
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

Dec 4 '05 #2
Joe
Great, thank you.

Joe

Bob Powell [MVP] wrote:
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


Dec 4 '05 #3
"Joe" <jh@thezac.cannedluncheonmeatobfuscator.com> schrieb
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

Dec 4 '05 #4
Joe
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

Armin Zingler wrote:
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

Dec 4 '05 #5
"Joe" <jh@thezac.cannedluncheonmeatobfuscator.com> schrieb
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
Dec 4 '05 #6
Joe
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
Armin Zingler wrote:
"Joe" <jh@thezac.cannedluncheonmeatobfuscator.com> schrieb
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

Dec 4 '05 #7
"Joe" <jh@thezac.cannedluncheonmeatobfuscator.com> schrieb
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

Dec 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: johngilmer | last post by:
I have three radio buttons that are possible answers to a question. I have put them in a table and there is a label that goes with each radio button as its text. I would like to make some of the...
0
by: mxkz | last post by:
Hi, I have been using VB6 for quite some time, but I've always had difficulty trying to place an outline around some text in a label. Using 6 or 8 labels around the one, with each at a...
4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
2
by: Dave | last post by:
How can I change the font size of a label? I have: Label1.Font.Size = "12" but it's telling me that property 'Size' is readonly. Thanks,
4
by: cashdeskmac | last post by:
I want to justify the text in a label control so that all of the text fills the label equally, but there don't seem to be any properties to set to allow this. Can anyone help?
0
by: SudhirKamath | last post by:
I have built a windows form using C#( .NET 2.0 ). The form has a label with text set to “:My label text.” When I build and run the form here everything looks fine. When a person is running the...
2
by: CharlieChilds | last post by:
Hi, I use Visual Web Developer 2008 Express. I have Label1 in a DataList ItemTemplate. I wish to use a button to get the text from that label and add it to text in TextBox1 which is elsewhere on...
3
by: Promedeus | last post by:
.NET Version: 3.5 Ok, I recieve an error (System.InvalidCastException was unhandled. Message="Unable to cast object of type 'System.Windows.Forms.Control' to type 'System.Windows.Forms.Label'.")...
6
by: sly9er | last post by:
Hello All, Does anyone know how to capture and save the font style of a label control. (FontStyle.Bold, FontStyle.Italic, FontStyle.Underline) I need to be able to save the font style and have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.