Font Class
All fonts in .NET are represented by a Font object. There are several constructors for this class, which gives you a good degree of flexibility when defining a font for use in your program. The ones you’ll use most often are shown here:
Font(name, size) Font(name, size, style) Font(name, size, unit) Font(name, size, style unit)
- The name argument is a string giving the name of the font family.
- The size argument specifies the size of the font.
- The style argument is a member of the FontStyle enumeration specifying a style: Bold, Italic, Regular, Strikeout, or Underline. To apply more than one style, combine them with the bitwise Or operator (|).
- The unit argument specifies the units of the size argument as a member of the GraphicsUnit enumeration. The default is points. Other values are shown in Table 1.
Table 1. Members of the GraphicsUnit Enumeration for Specifying Font Size
Member |
Unit |
Display |
1/75 inch |
Document |
1/300 inch |
Inch |
Inches |
Millimeter |
Millimeters |
Pixel |
Display pixels |
Point |
1/72 inch |
This example creates a Font object for Arial, 12 point font with no special style:
Font f = new Font("Arial", 12);
This one results in a 1/2 inch Times font, bold and underlined:
Font f = new Font("Times", 0.5f, FontStyle.Bold | FontStyle.Underline, GraphicsUnit.Inch);
A nice feature is that if the font name specified in the first argument does not exist, no error occurs. Rather the Font object is created using a default font. This avoids the problems that would otherwise occur if your program is installed on systems that do not have the font specified.