Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to write codes to get the correct font size that fits to the given box. Here I gave
VB
New Rectangle(0, 100, 100, 100)
as a new new rectangle.
But sometimes it dose not give the correct font size when there is two text lines in the box.
Help me to solve this or suggest any other solution.
THNAK YOU!

What I have tried:

VB
titleRect = New Rectangle(0, 100, 100, 100)
        Dim titleFont As Font
        Dim titleFontStyle As New FontStyle
        titleFont = New Font("Arial", 2, titleFontStyle)
dim articelTitle="THIS IS A TEST 1234567890"
   Dim titleFontSize As Integer = 2
        Do Until TextRenderer.MeasureText(articelTitle, titleFont).Width >= titleRect.Width Or TextRenderer.MeasureText(articelTitle, titleFont).Height >= titleRect.Height = True
            titleFontSize = titleFontSize + 1
            titleFont = New Font(fontName, titleFontSize - 2, titleFontStyle)

 Dim titleStringFormat As StringFormat = New StringFormat
        titleStringFormat.LineAlignment = StringAlignment.Center
        titleStringFormat.Alignment = StringAlignment.Center

        Loop
Posted
Updated 20-Mar-20 20:53pm
Comments
Richard MacCutchan 18-Mar-20 4:23am    
You need to divide the rectangle height by the number of lines in order to get the correct font height.
Member 13358124 18-Mar-20 4:33am    
THANK YOU!
I have no idea to write codes to get the number of lines.Could you please help me to do it.

1 solution

The following Code shows you a method (written by me) which calculates the possible Fontsize for a given Size - I think you can easily change it to a Rectangle :

Function GetFontSizeMatch(ByVal myText As String, ByVal myFont As Font, ByVal mySize As Size) As Single

     If Trim(myText).Length <= 0 Then myText = "X"

     Dim currentSize As Single = CSng(myFont.Size)
     Dim workFont As Font = New Font(myFont.Name, currentSize, myFont.Style)
     Dim myTextSize As SizeF

     If (mySize.Width >= 1) AndAlso (mySize.Height >= 1) Then
         Do
             currentSize += 4.0 : If currentSize > 50.0 Then Exit Do
             workFont = New Font(workFont.Name, currentSize, workFont.Style)
             myTextSize = TextRenderer.MeasureText(myText, workFont)
         Loop Until (myTextSize.Width > mySize.Width) Or (myTextSize.Height > mySize.Height)

         Do
             currentSize -= 0.5 : If currentSize < 5.0 Then Exit Do
             workFont = New Font(workFont.Name, currentSize, workFont.Style)
             myTextSize = TextRenderer.MeasureText(myText, workFont)
         Loop Until (myTextSize.Width <= mySize.Width) AndAlso (myTextSize.Height <= mySize.Height)

     End If

     Return currentSize

 End Function


There are 2 loops inside the Function. The first loop increases the Fontsize from your string as long as it reaches one of the bounds (with large steps). The second loop decreases the Fontsite with small steps to match inside the give Size. The result of this function is the maximum possible Fontsize for the given Size.
Good Luck ...
 
Share this answer
 
Comments
Member 13358124 21-Mar-20 3:22am    
Thank you very much...!
Ralf Meier 26-Mar-20 13:36pm    
You're welcome ...
Thanks for your voting ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900