17.5. Font Control This section introduces methods and constants related to font control. The properties of Font objects cannot be modified. If you need a different Font, you must create a new Font object. There are many overloaded versions of the Font constructor for initializing Font objects. Some properties of class Font are summarized in Fig. 17.8. Figure 17.8. Font class read-only properties.Property | Description |
---|
Bold | Returns true if the font is bold. | FontFamily | Returns the font's FontFamilya grouping structure to organize fonts and define their similar properties. | Height | Returns the height of the font. | Italic | Returns true if the font is italic. | Name | Returns the font's name as a String. | Size | Returns a float value indicating the current font size measured in design units (design units are any specified unit of measurement for the font). | SizeInPoints | Returns a float value indicating the current font size measured in points. | Strikeout | Returns TRue if the font is in strikeout format. | Underline | Returns true if the font is underlined. |
Note that the Size property returns the font size as measured in design units, whereas SizeInPoints returns the font size as measured in points (the more common measurement). Design units allow the font size to be specified in one of several units of measurement, such as inches or millimeters. Some versions of the Font constructor accept a GraphicsUnit argument. GraphicsUnit is an enumeration that allows you to specify the unit of measurement that describes the font size. Members of the GraphicsUnit enumeration include Point (1/72 inch), Display (1/75 inch), Document (1/300 inch), Millimeter, Inch and Pix el. If this argument is provided, the Size property contains the size of the font as measured in the specified design unit, and the SizeInPoints property contains the corresponding size of the font in points. For example, if we create a Font having size 1 and specify the unit of measurement as GraphicsUnit.Inch, the Size property will be 1 and the SizeInPoints property will be 72, because there are 72 points in an inch. The default measurement for the font size is GraphicsUnit.Point (thus, the Size and SizeInPoints properties will be equal). Class Font has several constructors. Most require a font name, which is a String representing a font currently supported by the system. Common fonts include Microsoft SansSerif and Serif. Most Font constructors also require as arguments the font size and font style. The font style is specified with a constant from the FontStyle enumeration (Bold, Italic, Regular, Strikeout and Underline, or a combination of these). You can combine font styles with the Or operator, as in FontStyle.Italic Or FontStyle.Bold, which makes a font both italic and bold. Graphics method DrawString sets the current drawing fontthe font in which the text displaysto its Font argument. Common Programming Error 17.1 | Specifying a font that is not available on a system is a logic error. If this occurs, the system's default font is substituted. |
Drawing Strings in Different Fonts The program in Fig. 17.9 displays text in different fonts and sizes. The program uses the Font constructor to initialize the Font objects (lines 11, 15, 19 and 23). Each call to the Font constructor passes a font name (e.g., Arial, Times New Roman, Courier New or Tahoma) as a String, a font size (a float) and a FontStyle object (style). Graphics method DrawString sets the font and draws the text at the specified location. Note that line 7 creates a DarkBlue SolidBrush object (brush). All Strings drawn with that brush appear in DarkBlue. Figure 17.9. Fonts and FontStyles. 1 ' Fig. 17.9 FrmUsingFonts.vb 2 ' Fonts and FontStyles. 3 Public Class FrmUsingFonts 4 ' demonstrate various font and style settings 5 Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) 6 Dim graphicsObject As Graphics = paintEvent.Graphics 7 Dim brush As New SolidBrush(Color.DarkBlue) 8 9 ' arial, 12 pt bold 10 Dim style As FontStyle = FontStyle.Bold 11 Dim arial As New Font("Arial", 12, style) 12 13 ' times new roman, 12 pt regular 14 style = FontStyle.Regular 15 Dim timesNewRoman As New Font("Times New Roman", 12, style) 16 17 ' courier new, 16 pt bold and italic 18 style = FontStyle.Bold Or FontStyle.Italic 19 Dim courierNew As New Font("Courier New", 16, style) 20 21 ' tahoma, 18 pt strikeout 22 style = FontStyle.Strikeout 23 Dim tahoma As New Font("Tahoma", 18, style) 24 25 graphicsObject.DrawString(arial.Name & _ 26 " 12 point bold.", arial, brush, 10, 10) 27 28 graphicsObject.DrawString(timesNewRoman.Name & _ 29 " 12 point plain.", timesNewRoman, brush, 10, 30) 30 31 graphicsObject.DrawString(courierNew.Name & _ 32 " 16 point bold and italic.", courierNew, brush, 10, 50) 33 34 graphicsObject.DrawString(tahoma.Name & _ 35 " 18 point strikeout.", tahoma, brush, 10, 70) 36 End Sub ' OnPaint 37 End Class ' FrmUsingFonts | Font Metrics You can determine precise information about a font's metrics (or properties), such as height, descent (the amount characters dip below the baseline), ascent (the amount characters rise above the baseline) and leading (the difference between the ascent of one line and the descent of the previous line). Figure 17.10 illustrates these font metrics. Figure 17.10. Font metrics illustration. Class FontFamily defines characteristics common to a group of related fonts. Class FontFamily provides several methods used to determine the font metrics shared by members of a particular family. These methods are summarized in Fig. 17.11. Figure 17.11. FontFamily methods that return font-metric information.Method | Description |
---|
GetCellAscent | Returns an Integer representing the ascent of a font as measured in design units. | GetCellDescent | Returns an Integer representing the descent of a font as measured in design units. | GetEmHeight | Returns an Integer representing the height of a font as measured in design units. | GetLineSpacing | Returns an Integer representing the distance between two consecutive lines of text as measured in design units. | The program in Fig. 17.12 displays the metrics of two fonts. Line 10 creates Font object arial and sets it to 10-point Arial font. Line 11 uses Font property FontFamily to obtain object arial's FontFamily object. Lines 1415 output the String representation of the font. Lines 1727 then use methods of class FontFamily to obtain the ascent, descent, height and leading of the font and draw strings containing this information. Lines 3050 repeat the process for font sansSerif, a Font object derived from the MS Sans Serif FontFamily. Figure 17.12. FontFamily class used to obtain font-metric information. 1 ' Fig 17.12: FrmUsingFontMetrics.vb 2 ' Displaying font metric information 3 Public Class FrmUsingFontMetrics 4 ' displays font information 5 Protected Overrides Sub OnPaint(ByVal paintEvent As PaintEventArgs) 6 Dim graphicsObject As Graphics = paintEvent.Graphics 7 Dim brush As New SolidBrush(Color.DarkBlue) 8 9 ' Arial font metrics 10 Dim arial As New Font("Arial", 10) 11 Dim family As FontFamily = arial.FontFamily 12 13 ' display Arial font metrics 14 graphicsObject.DrawString("Current Font: " & _ 15 arial.ToString(), arial, brush, 10, 10) 16 17 graphicsObject.DrawString("Ascent: " & _ 18 family.GetCellAscent(FontStyle.Regular), arial, brush, 10, 30) 19 20 graphicsObject.DrawString("Descent: " & _ 21 family.GetCellDescent(FontStyle.Regular), arial, brush, 10, 50) 22 23 graphicsObject.DrawString("Height: " & _ 24 family.GetEmHeight(FontStyle.Regular), arial, brush, 10, 70) 25 26 graphicsObject.DrawString("Leading: " & _ 27 family.GetLineSpacing(FontStyle.Regular), arial, brush, 10, 90) 28 29 ' display Sans Serif font metrics 30 Dim sanSerif As New Font("Microsoft Sans Serif", _ 31 12, FontStyle.Italic) 32 family = sanSerif.FontFamily 33 34 graphicsObject.DrawString("Current Font: " & _ 35 sanSerif.ToString(), sanSerif, brush, 10, 130) 36 37 graphicsObject.DrawString("Ascent: " & _ 38 family.GetCellAscent(FontStyle.Regular), _ 39 sanSerif, brush, 10, 150) 40 41 graphicsObject.DrawString("Descent: " & _ 42 family.GetCellDescent(FontStyle.Regular), _ 43 sanSerif, brush, 10, 170) 44 45 graphicsObject.DrawString("Height: " & _ 46 family.GetEmHeight(FontStyle.Regular), sanSerif, brush, 10, 190) 47 48 graphicsObject.DrawString("Leading: " & _ 49 family.GetLineSpacing(FontStyle.Regular), _ 50 sanSerif, brush, 10, 210) 51 End Sub ' OnPaint 52 End Class ' FrmUsingFontMetrics | |