From: Claire on
Hello,
I have an array of labels, which height changes in relation to
the form's height (form is resizable).
What will be the best method of selecting the labels caption font size?
Thanks,
Claire


From: CY on
On 7 Feb, 04:46, "Claire" <replyto(a)fra> wrote:
> Hello,
>             I have an array of labels, which height changes in relation to
> the form's height (form is resizable).
> What will be the best method of selecting the labels caption font size?
> Thanks,
> Claire

Might be an answer here http://www.dreamincode.net/forums/showtopic34776.htm
and yes its VB6 code... but the magic *8 will have to be adjusted for
a different font/size

Just put the text "The Quick Brown Fox Jumps Over The Lazy Dog
1234567890" in a textbox and try it out, if having multiple lines

The
Quick
Brown
Fox
Jumps
Ower
The
Lazy
Dog
1234567890

Then count the vbCRLF or vbLF in the text to get number of lines
If you put in scrollbars, then adjust for the size they take, if there
can be scroll bars on a label... can activate it in VBA but only get
one (Horizontal) in runtime...
Sorry dont have VB6 at home.. vbCRLF only get me a pi sign...

//CY
From: Larry Serflaten on

"Claire" <replyto(a)fra> wrote
> Hello,
> I have an array of labels, which height changes in relation to
> the form's height (form is resizable).
> What will be the best method of selecting the labels caption font size?

The best method would be to calculate the size you need based on the
size of the resized lable. But, as you increase the font size, the text
width and height both change. You did not mention of the labels are
to be fixed width or autosize with the size of the text.

You will have to determine the best methods to use, based on your
specific needs, but perhaps the code below will get you started.

If you uncomment the MAX line, then the full text is always visible
for a fixed width label. The value was determined by setting the
desired font (Name) in the IDE property box, and then running
the program to see what font size caused the text to overrun the
label. That value was then entered in for the MAX value.

Or, leaving the MAX line commented, you could set the Label's
AutoSize property to True, if you want the labels to expand to
show all the text.

Again, you decide what you need for your situation. To run the
code, start a new project and add 1 Label control, setting its Index
property to 0. Then paste in the code below and try it out....

Note that the resize code only alters the font size for Label1(0).
They all change because they were all set to using that font object
in the form load routine. That is one way to manage fonts on
multiple controls....

HTH
LFS

Option Explicit

Private Sub Form_Load()
Dim lbl As Long, tbh As Long
Label1(0).Move 90, 90, 2400, 240
Label1(0).BackColor = vbWhite
Label1(0).Caption = "A B C D E F G"
' Make more labels
For lbl = 0 To 5
If lbl > 0 Then
Load Label1(lbl)
Set Label1(lbl).Font = Label1(0).Font
Label1(lbl).Visible = True
End If
Label1(lbl).Move 90, lbl * 270 + 30
Next
' Set Form size
tbh = Me.Height - Me.ScaleHeight ' Title Bar height
Me.Height = Label1.Count * 270 + 30 + tbh
End Sub

Private Sub Form_Resize()
Dim gap&, lbl As Long
Dim hgt!, siz As Single
Const MAX = 18 ' for MS Sans Sarif

gap = Label1.Count * 30 + 30
hgt = (Me.ScaleHeight - gap) / Label1.Count
If hgt < 1 Then hgt = 1
' Resize labels
For lbl = 0 To Label1.UBound
Label1(lbl).Move 90, lbl * (hgt + 30) + 30, Label1(0).Width, hgt
Next
' Calculate font size
siz = Me.ScaleY(hgt - 30, vbTwips, vbInches) * 72
If siz < 3 Then siz = 3
'If siz > MAX Then siz = MAX
Label1(0).Font.Size = siz
Debug.Print siz, Label1(0).Font.Size
End Sub


From: Mike Williams on
"Claire" <replyto(a)fra> wrote in message
news:eTNXog6pKHA.1672(a)TK2MSFTNGP04.phx.gbl...

> I have an array of labels, which height changes in relation
> to the form's height (form is resizable). What will be the
> best method of selecting the labels caption font size?

Although a Label's Height property can only take up a value that equates to
an integer twip value (regardless of the ScaleMode you are using), its
actual displayed height (as with all things displayed on your Form) is
always a number of whole pixels, so the first thing you need to calculate is
the actual displayed pixel height of the Label after you have set its size
in whatever scale units you are using. You can do this by setting you label
to the desired height and then casting its height in pixels into a Long so
that VB performs the appropriate rounding for you, as in High =
Me.ScaleY(Label2.Height, Me.ScaleMode, vbPixels), where the variable High is
a Long.

If you then want to ensure that the font point size you select will always
fit into the actual pixel height of the Label (something you may want to do
if the layout of your Form is too "tight" to permit small increases in the
Label's size) then you would be better off using the GDI CreateFontIndirect
function to create a font of the appropriate font name and other attributes,
specifying the desired size using the pixel value you have just calculated.
Do not negate the pixel value (as is common in many cases when dealing with
createFontindirect) , but pass it to CreateFontDirect as it stands, as a
positive number. The reason I mention this is because generally when
creating a font of a specific desired point size using CreateFontindirect it
is usual to convert the point size to pixels and then negate that value,
passing the negative pixel value to CreateFontIndirect in order to achieve
the desired font size. However, to fit the font into our Label we are
interested in the overall height of the returned font (its TextHeight)
rather than its actual point size height, and so we pass a positive value.
This causes CreateFontindirect to create a font of such a point size that
its total overall TextHeight, including both its "point size" height and its
"Internal Leading" height, both fit inside the pixel height we have passed
to it, and therefore fit properly inside our Label. You then assign the
returned font to something that has a hDC and then use the GDI
GetTextMetrics function to read the total of its Ascent and Descent values,
converting the returned pixel "Ascent plus Descent" value to Points using
the VB ScaleY function, giving you the point size you need so that you can
set the Label's font size accordingly.

The above method is a bit convoluted of course, but it does take everything
into account and it ensures that the font will fit into your Label without
you needing to adjust its height to some other greater value, as might be
the case with some other methods. If you want a much simpler way, but one
that requires you to use a small code loop and which therefore is not quite
as fast, then you can simply set the point size of a DC (TextBox or
something using the same font name and attributes) to an approximate
calculated value in accordance with the Label's calculated point height and
then check the actual point size of the font that VB gives you, also
checking whether the resultant TextHeight of the font is greater or less
than the desired value. Then use a code loop to increase or decrease the
font size by a small amount, say half a point each time, repeatedly checking
until the Textheight is less than or equal to the Label's height.

Another very simple way that works well if you are happy to allow the
Label's Height to adjust itself by a couple of pixels or so either way from
your actual desired height (which might be okay depending on the tightness
of the layout of your Form) is to use an Autosize Label and each time you
change its Height property follow the height change with the following code
to change its font size:

Label1.Font.Size = ScaleY(Label1.Height, _
Label1.Container.ScaleMode, vbPoints) * 0.88

The "0.88" is a magic number which approximately takes account of the
relationship between TextHeight and "point size height" for many typical
TrueType fonts at the sizes you are likely to be using (0.80 is probably a
bit nearer the mark for typical screen fonts), and it ensures that the
Autosize mechanism of the Label will not adjust the height of the label too
far from the height you had actually set it to. All fonts of course are
restricted to the equivalent of whole pixel values for both the actual point
size and for the additional internal leading size, and screen fonts (as
opposed to TrueType fonts) have other restrictions as well, and so the
closenesss to your desired value will be better for trueType fonts than for
screen fonts.

Mike




From: Mike Williams on
"Mike Williams" <Mike(a)WhiskyAndCoke.com> wrote in message
news:uQ60VD$pKHA.4532(a)TK2MSFTNGP05.phx.gbl...

I've just noticed that in one of the suggested methods in my previous
response I said, ". . . you can simply set the point size of a DC (TextBox
or something using the same font name and . . .". I actually meant to say a
PictureBox, not a TextBox. Also, regardless of which method you choose you
need to consider the width as well as the height of the text, but of course
only you will know which of those two measurements is the most important in
your own specific case because it depends on the layout of your Form and the
other things it contains and the kind of resizing you allow.

Mike