2007-06-07
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 3 of 3 )
Line Spacing
When you display multiple lines of text on a form, you must control the vertical spacing between lines. As I have already mentioned, one way to do this is to use the height value that is returned by the MeasureString method. Here's an example:
Dim xPos As Integer = 100
Dim yPos As Integer = 100
Dim msg As String = "Message"
Dim sz1 As SizeF = g.MeasureString(msg, f)
Dim i As Integer
For i = 1 To 3
g.DrawString("Message", f, Brushes.Black, xPos, yPos)
yPos += sz1.Height
Next
The output is shown in Figure 4. This also displays lines (the code to draw these is not shown) that delineate the left end of the display rectangle for each line.
Another way to control line height is with text metric information returned from a FontFamily object (I told you I'd get to this!), specifically the LineSpacing metric. Assuming that ff is your FontFamily and f is your font, here's the formula:
LineSpacingInDisplayUnits = ff.GetLineSpacing(FontStyle.Regular) * _
f.Size / ff.GetEmHeight(FontStyle.Regular)
You can see how the em height is used as a conversion factor.
This method of determining line spacing gives somewhat smaller spacing that does MeasureString. Figure 5 compares the two methods—the lines in black were spaced using MeasureString while the lines in red were spaced using the method described here.
More Precise Text Placement
The techniques that I have covered so far are fine for many text applications, but do not provide for really precise placement of text. For example, perhaps you need to line up the left edge and baseline of the text with precise positions, as shown in Figure 6. This sort of text placement is much more difficult, and we'll explore the required techniques in the second article of this series.
![]() |
|


