By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

Label Font Size

P: n/a
Is there a way to / or a mathematical formula to see if a font size is 'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point, the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way of
a mathematical formula to do this in one shot.

Thanks,

miro

Oct 24 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Oct 24, 6:57*am, "Miro" <m...@beero.comwrote:
Is there a way to / or a mathematical formula to see if a font size is 'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
* * * * Label2.Font = New Font(Label2.Font.Name, _
* * * *Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point, the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way of
a mathematical formula to do this in one shot.

Thanks,

miro
I'm not sure but you may want to enable Label's "AutoSize" property to
True because if you keep it disabled, the label borders will remain
fixed regardless of label's font. By the way, based on your
description, it's clueless that why you're using the code while
resisizng form.

Thanks,

Onur Güzel
Oct 24 '08 #2

P: n/a
"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is fixed,
but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the width)
use TextRenderer.MeasureText with the font from the label.

Good luck

Oct 24 '08 #3

P: n/a
Yeah I have started looking at finding the ratio,

Another thought I had is to do some benchmarks in some if statements - that
might be easier if i cannot find the ratio easily.

Thanks for your help...
Ill be playing with this this weekend.

Miro

"Clive Lumb" <clumb2@gratuit_en_anglais.fr.invalidwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
>Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is fixed,
but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the
width) use TextRenderer.MeasureText with the font from the label.

Good luck
Oct 24 '08 #4

P: n/a
Autosize sizes the labels borders to the font size

I am trying to size the fontsize to the labels borders.

I dont see how it is clueluess...as I am trying to enlarge and shrink a
label on the screen depending on the realistate.

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:6d**********************************@m32g2000 hsf.googlegroups.com...
On Oct 24, 6:57 am, "Miro" <m...@beero.comwrote:
Is there a way to / or a mathematical formula to see if a font size is
'too
big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize that
font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the
font is simply too big for the label.
I know I can add some math for a maximum font size compared to the height
and width of the label, but im wondering if there is another simpler way
of
a mathematical formula to do this in one shot.

Thanks,

miro
I'm not sure but you may want to enable Label's "AutoSize" property to
True because if you keep it disabled, the label borders will remain
fixed regardless of label's font. By the way, based on your
description, it's clueless that why you're using the code while
resisizng form.

Thanks,

Onur Güzel

Oct 24 '08 #5

P: n/a
I have found a very simple solution to my problem - just posting it so if
anyone else wants to try this, they can.

It's based off of what Clive Lumb said.

I temporarily put 3 textboxes on the screen. On resize i just put the form
width and height and fontsize there so i can watch the size setting.
Basically I found ( with my window layout ) font size 232 was teh biggest
font size i wanted.

Then I took the font size on an inbetween window size - and divided it by
the form height and got a 66% font size to height ratio
then I took the font size and divided it by the width and got a 22% font
size to width ratio.

In the end...I take the height and multiply it by 66%
take the height and multiply it by the 21%
take the minimum of the two

works like a charm.

Thanks Clive - you got me on the right track.

the textbox's will be removed but I left them there so you get the idea.
===
TextBox1.Text = Me.Size.Width.ToString
TextBox2.Text = Me.Size.Height.ToString

Dim CalcFontSizeA As Integer = Math.Min(232,
CType(Math.Round(Me.Label2.Height * 0.66, 0), Integer))
Dim CalcFontSizeB As Integer = Math.Min(232,
CType(Math.Round(Me.Label2.Width * 0.21, 0), Integer))

Dim FinalFontSize As Integer = Math.Min(CalcFontSizeA,
CalcFontSizeB)
Label2.Font = New Font(Label2.Font.Name, FinalFontSize,
Label2.Font.Style) )

TextBox3.Text = Label2.Font.Size.ToString
===

Thank you,

Cheers'

Miro

"Miro" <mi**@beero.comwrote in message
news:ud**************@TK2MSFTNGP02.phx.gbl...
Yeah I have started looking at finding the ratio,

Another thought I had is to do some benchmarks in some if statements -
that might be easier if i cannot find the ratio easily.

Thanks for your help...
Ill be playing with this this weekend.

Miro

"Clive Lumb" <clumb2@gratuit_en_anglais.fr.invalidwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>"Miro" <mi**@beero.coma écrit dans le message de news:
ei**************@TK2MSFTNGP04.phx.gbl...
>>Is there a way to / or a mathematical formula to see if a font size is
'too big' for a label.

I have a label that is docked to 'fill' a form, and I want to resize
that font based on the width and height of the label.

So far I have something like this on the Form_Resize event:
Label2.Font = New Font(Label2.Font.Name, _
Label2.Size.Height - 100, Label2.Font.Style)

but it only works to a point, once the height gets past a certain point,
the font is simply too big for the label.
I know I can add some math for a maximum font size compared to the
height and width of the label, but im wondering if there is another
simpler way of a mathematical formula to do this in one shot.

Thanks,
You want to be looking at the font's height to size ratio (which is
fixed, but varies from font to font)
In pseudocode you need to do this
Ratio = font.height/font.size
New font size= label.height/Ratio

If you need to adjust the width to the content (or the content to the
width) use TextRenderer.MeasureText with the font from the label.

Good luck
Oct 25 '08 #6

P: n/a
You need to look towards System.Drawing.Graphics.MeasureString.

This method should give you the length of the string as it will be
drawn on the form. It accepts a Font argument so as to better
pinpoint the exact size. I've worked with it before and its not
perfect, but pretty decent.

Oct 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.