Link to home
Start Free TrialLog in
Avatar of william007
william007

asked on

Function for calculating variable width font

I wish to write a underline function that can use the indicated character to underline the certain text, eg

This is the example
=============

In this example, the function simply append a carriage return and add 13 "=".


For fixed width font like courier, we know that "This is the example" has 13 characters, we can easily use 13 "=" to underline it.

The problem is for variable width font like Times New Roman. Arial, etc, we cannot be certain that how many "=" we should use in order to underline certain text perfectly.

Is there any written function on the web that can help us to calculate this based on the font type?
(For example, If we input "Times new roman", <font size>, "This is the example", and "=" as parameter to the function, it should return approximately how many "=" should be use based on the length of the text)
(Or, If we input "Times new roman", <font size>, "This is the example"  to the function, the function should return us the length of string based on certain unit, and we use the return information to calculate our own)
(Or even there don't have any function avaialable, but there is a table of the width of font for certain font type, we write the function ourself)
(Or, something that is workable)

Thanks:-)
SOLUTION
Avatar of Farzad Akbarnejad
Farzad Akbarnejad
Flag of Iran, Islamic Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of william007
william007

ASKER

Thanks:-)
Something like this might be on the track you're looking for.  It's definitely not the most efficient way of doing it though.

Public Function UnderlineMyText(ByVal FontName As String, ByVal FontSize As Double, ByVal TextString As String, ByVal UnderlineChar As String)
        Dim G As Graphics = Me.CreateGraphics   'Graphic objects
        Dim F As Font = New Font(FontName, FontSize)  'Font object
        Dim WidthOfText As Double = 0  'Width of the longest string (in twips)
        Dim WidthOfUnderline As Double = 0 'Width of the Underline (in twips)
        Dim strUnderline as String = Nothing

        'Get the twips width of the longest string
        WidthOfText = G.MeasureString(TextString, F).Width

        While WidthOfUnderline < WidthOfText
            strUnderline &= UnderlineChar
            WidthOfUnderline = G.MeasureString(strUnderline, F).Width
        End While
End Function