Results 1 to 4 of 4

Thread: Label Font(size autosize)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    Label Font(size autosize)

    Hi,

    What code should I write so that the Size of the font in a Label will automatically adjust?? base on the width and height of the label... The font size will change not the the Label..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Label Font(size autosize)

    You would need to use code similar to this:
    VB Code:
    1. Private Sub Label1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.Resize
    2.         Me.Label1.Font = New Font(Me.Label1.Font.FontFamily, Me.Label1.Height / 2)
    3.     End Sub
    The second argument to the Font constructor is the point size. You might want to use a more complex method of calculating it that takes Height and Width into account. A bit of trial and error should do the job.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Label Font(size autosize)

    Well this is the ugly mess that I have come up with, its far from optimized but you should get the idea, seems to work ok (ish).

    Its a sub that you need to call from both the textchanged and resize events of the label... I have included the events so you can see how to do it.

    Ideally you would build this functionality into a new control that is derived from Label.

    Enjoy.

    VB Code:
    1. Private Sub FitLabelText(ByVal sender As Object)
    2.  
    3.         Dim lab As Label = CType(sender, Label)
    4.  
    5.         Dim gr As Graphics = lab.CreateGraphics
    6.  
    7.         Dim sFont As Font
    8.         Dim layoutSize As SizeF = New SizeF(lab.Width, Label1.Height)
    9.         Dim sf As StringFormat = New StringFormat(StringFormatFlags.DirectionVertical)
    10.         Dim charsFitted As Integer
    11.         Dim lines As Integer
    12.         Dim sSize As New SizeF
    13.  
    14.         Dim i As Integer
    15.  
    16.         For i = 8 To 200          '8 is the minimum font size, 200 is the max
    17.             sFont = New Font(lab.Font.Name, i, lab.Font.Style)
    18.             sSize = gr.MeasureString(lab.Text, sFont, layoutSize, sf, charsFitted, lines)
    19.             If charsFitted < lab.Text.Length Then Exit For
    20.         Next i
    21.  
    22.         lab.Font = New Font(lab.Font.Name, i - 1, lab.Font.Style, GraphicsUnit.Pixel)
    23.  
    24.     End Sub
    25.  
    26.     Private Sub Label1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Resize
    27.  
    28.         FitLabelText(sender)
    29.  
    30.     End Sub
    31.  
    32.     Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
    33.  
    34.         FitLabelText(sender)
    35.  
    36.     End Sub
    I don't live here any more.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Label Font(size autosize)

    PS, my version tries to fit the entire string into the label area, the text can be very tiny if its a long message and a small label.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width