What if you want to change or modify the font of a control in a windows application? The System.Drawing.Font class allows you to create fonts that you can use or assign to the Font properties of different controls or components. The following table shows some useful properties of the Font class.
|Specifies whether the font is bold.
|Specifies the font family used by the font.
|Specifies the line spacing of the font.
|Specifies whether the font is italicized.
|Specifies the face name of the font.
|Specifies the size of the font using the unit specified by the Unit property.
|Specifies whether the font has a horizontal line in through the font.
|Specifies the style or styles applied to the font.
|Specifies whether the font has a line underneath it.
|Specifies the unit to be used when specifying the size of the font.
Figure 1 – System.Drawing.Font Properties
Creating a Font Object
The following code calls the constructor of the Font class and uses the Times New Roman font family and size 12.
Font myFont = new Font("Times New Roman", 12);
The font family specifies the appearance of the font. Common font families include Arial, Verdana, Courier New, Times New Roman, and Sans Serif. Note that before using a font family for a Font object, the actual font must be installed in your system. The previously mentioned font families are already installed in a Windows system by default.
You can also use the System.Drawing.FontFamily class to create a FontFamily object using a specified family name. You can then use an overloaded constructor of the Font class and pass the FontFamily object to it.
FontFamily family = new FontFamily("Arial");
Font myFont = new Font(family, 12);
Using the FontFamily object allows you to use some of its method such as GetCellAscent(), GetCellDescent(), GetEmHeight(), and GetLineSpacing().
Another constructor allows you to specify the style of the font. We can use values from the System.Drawing.FontStyle enumeration. The following creates a bold font.
Font myFont = new Font("Verdana", 14, FontStyle.Bold);
Another useful constructor of the Font class allows you to use the properties of another Font object and then specify a new style that will be applied to it. Suppose we have a label that we want to italicize. We can pass the Font of the label and then use the FontStyle.Italic value.
Font myFont = new Font(label1.Font, FontStyle.Italic);
You can also specify the unit to be used by the size. For example, size 12 can either be 12 inches, 12 pixels, or 12 millimeters. We can use the values of the System.Drawing.GraphicsUnit enumeration and another constructor of the Font object. The following code creates a font with size of 12 pixels.
Font myFont = new Font("Times New Roman", 12, GraphicsUnit.Pixel);
We can then assign our created font to the Font property of a control such as a label to change its font based on the values we have given when creating the Font object.
label1.Font = myFont;
There are many font families you can use for your font depending on which fonts are installed in your system. To see which fonts are available, in Windows 7, go to Start > Control Panel > Appearance and Personalization > Fonts. The following screenshot shows you some example of font families and how they look.
Figure 2 – Different Font Families
You can either pass the name of the font family as string or create a FontFamily object when creating the Font object.
Font myFont = new Font("Consolas", 14);
Font myFont = new Font(new FontFamily("Consolas"), 14);
When dealing with font sizes, there are two things to consider, the actual size and the units to use. We can use the Size property of the font to specify the font size.
myFont.Size = 12;
We can determine what unit to use by using the values from the System.Drawing.GraphicsUnit enumeration which contains the following.
|Specifies the unit of measure of the display device. Typically pixels for video displays, and 1/100 inch for printers.
|Specifies a device pixel as the unit of measure.
|Specifies a printer’s point (1/72 inch) as the unit of measure.
|Specifies the inch as the unit of measure.
|Specifies the document unit (1/300 inch) as the unit of measure.
|Specifies the millimeter as the unit of measure.
|Specifies the world coordinate system unit as the unit of measure.
Figure 3 – System.Drawing.GraphicsUnit
You can use the Unit property of the Font class to specify the unit to use when sizing the font.
myFont.Unit = GraphicsUnit.Pixel;
We can apply different font styles to a font. The Font class contains some property which accepts a boolean value specifying if a certain style is applied. For example, you can use the Bold property and set it to true to make a font bold, or the Italic property to italicized a font.
myFont.Bold = true;
myFont.Italicized = true;
The code above makes the font bold and italicized at the same time. You can also use the Strikeout and Underline properties to apply those styles to a font.
You can also use the values of the System.Drawing.FontStyles enumeration which contains different font styles you can apply to a font.
|Text with a line through the middle.
Figure 4 – System.Drawing.FontStyle Enumeration
The following screenshot shows you how each style will look.
Figure 5 – Different Font Styles
We can use a constructor of the Font class to apply a new style.
myFont = new Font(myFont, FontStyle.Bold);
We can also apply multiple styles to the font by using the bitwise OR operator.
myFont = new Font(myFont, FontStyle.Bold | FontStyle.Italic);
The line above applies both bold and italic font styles.
The Styles property of the Font allows us to get the styles applied to a font. If we want to remove a particular style from a font with multiple styles, then we can use the bitwise XOR operator.
myFont = new Font(myFont, myFont.Style ^ FontStyle.Bold);
The bitwise XOR operator can also serve as a toggle mechanism because if removes a style if it exist as a style of the font or applies the style if it is not yet applied to a font.
The bitwise AND operator is useful if you want to check if a certain style is already applied to a font.
if ( (myFont.Style & FontStyle.Bold) == FontStyle.Bold)
MessageBox.Show("The font is bold.");
Notice that we enclosed the expression in parentheses because the == operator has higher precedence. We tested if the result of the expression is equal to the style being tested.