Label Font Size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miro

    Label Font Size

    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.Fon t.Name, _
    Label2.Size.Hei ght - 100, Label2.Font.Sty le)

    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

  • kimiraikkonen

    #2
    Re: Label Font Size

    On Oct 24, 6:57 am, "Miro" <m...@beero.com wrote:
    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.Fon t.Name, _
           Label2.Size.Hei ght - 100, Label2.Font.Sty le)
    >
    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

    Comment

    • Clive Lumb

      #3
      Re: Label Font Size

      "Miro" <miro@beero.com a écrit dans le message de news:
      eiHpnyYNJHA.387 6@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.Fon t.Name, _
      Label2.Size.Hei ght - 100, Label2.Font.Sty le)
      >
      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.Me asureText with the font from the label.

      Good luck



      Comment

      • Miro

        #4
        Re: Label Font Size

        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:uLGR50aNJH A.1160@TK2MSFTN GP02.phx.gbl...
        "Miro" <miro@beero.com a écrit dans le message de news:
        eiHpnyYNJHA.387 6@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.Fon t.Name, _
        > Label2.Size.Hei ght - 100, Label2.Font.Sty le)
        >>
        >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.Me asureText with the font from the label.
        >
        Good luck
        >
        >
        >

        Comment

        • Miro

          #5
          Re: Label Font Size

          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.



          "kimiraikko nen" <kimiraikkonen8 5@gmail.comwrot e in message
          news:6d9de653-42d4-4952-91af-776d452a3e70@m3 2g2000hsf.googl egroups.com...
          On Oct 24, 6:57 am, "Miro" <m...@beero.com wrote:
          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.Fon t.Name, _
          Label2.Size.Hei ght - 100, Label2.Font.Sty le)
          >
          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

          Comment

          • Miro

            #6
            Re: Label Font Size - Solved

            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.T oString
            TextBox2.Text = Me.Size.Height. ToString

            Dim CalcFontSizeA As Integer = Math.Min(232,
            CType(Math.Roun d(Me.Label2.Hei ght * 0.66, 0), Integer))
            Dim CalcFontSizeB As Integer = Math.Min(232,
            CType(Math.Roun d(Me.Label2.Wid th * 0.21, 0), Integer))

            Dim FinalFontSize As Integer = Math.Min(CalcFo ntSizeA,
            CalcFontSizeB)


            Label2.Font = New Font(Label2.Fon t.Name, FinalFontSize,
            Label2.Font.Sty le) )

            TextBox3.Text = Label2.Font.Siz e.ToString
            ===

            Thank you,

            Cheers'

            Miro

            "Miro" <miro@beero.com wrote in message
            news:ud0V80dNJH A.4760@TK2MSFTN GP02.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:uLGR50aNJH A.1160@TK2MSFTN GP02.phx.gbl...
            >"Miro" <miro@beero.com a écrit dans le message de news:
            >eiHpnyYNJHA.387 6@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.Fon t.Name, _
            >> Label2.Size.Hei ght - 100, Label2.Font.Sty le)
            >>>
            >>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.Me asureText with the font from the label.
            >>
            >Good luck
            >>
            >>
            >>
            >

            Comment

            • cfps.Christian

              #7
              Re: Label Font Size

              You need to look towards System.Drawing. Graphics.Measur eString.

              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.

              Comment

              Working...