Label Font Size

M

Miro

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
 
K

kimiraikkonen

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
 
C

Clive Lumb

Miro said:
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
 
M

Miro

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.



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
 
M

Miro

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
 
M

Miro

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
 

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

Top