The Style of Text

Introduction

To enhance the appearance of a character, text can use some effects that are referred to as a style. To let you specify a style when creating a font, the Font class is equipped with the following constructor:

public Font(string familyName, float emSize, FontStyle style);

The first argument is the name of the font. The second argument is the size of the font. The third argument is of type FontStyle, which is an enumeration.

Practical LearningPractical Learning: Introducing .NET Framework Collections

  1. Start Microsoft Visual Studio
  2. Create a Windows Forms App named TextFormatting

A Regular Text

Text is referred to as regular if it displays "normally" without any special effect. To support this aspect, the FontStyle enumeration is equipped with a member named Regular. To indicate this aspect, you can either omit a style as we have done so far, or use the above constructor and specify the style argument as FontStyle.Regular.

An Underline Text

Normally, when you write text, you simply align the characters, words, and punctuation signs as you judge them necessary. As one way to make a section or line stand out, you can draw a line below that character, word, or line. To help you do this, the FontStyle provides a member named Underline. Therefore, to display a line below text that uses your font, pass the last argument of the above constructor as FontStyle.Underline. Here is an example:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        private Font? fntWrite;

        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitleRegular = new Label();

            fntWrite = new Font("Footlight MT Light", 41.218F, FontStyle.Underline);

            lblTitleRegular.Text = "Honor and Sacrifice";
            lblTitleRegular.AutoSize = true;
            lblTitleRegular.Location = new Point(12, 25);
            lblTitleRegular.Font = fntWrite;
            Controls.Add(lblTitleRegular);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            fntWrite = new Font("Footlight MT Light", 41.218F, FontStyle.Underline);

            e.Graphics.DrawString("Honor and Sacrifice", fntWrite,
                                  Brushes.Black, 12.00F, 128.824F);
        }
    }
}

This would produce:

Drawing Strings - The Style of Text - Underline

If text exists already and you want to find out whether it is underlined, to help you, the Font class is equipped with a read-only Boolean property named Underline:

public bool Underline { get; }

Thfind out whether a character or a word is italicized, get the Boolean value of theproperty of its fontBoolean value of the Underline property ofm its font

An Italicizeded Text

By default, a letter or a digit is written standing up. To make it somehow fancy, a letter, a digit, or a group of symbols can be written leaned or somehow forward diagonally. Such a symbol or group of symbols is said to be italicised. Some fonts are designed with an italic appearance. Therefore, when planning to display text on a control or a document, if you want all characters to be italized, you can select such a font. Here is an example for a font named Brush Script MT (found in MS Windows 11):

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        private Font? fntWrite;

        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitleRegular = new Label();

            fntWrite = new Font("Brush Script MT", 44.218F);

            lblTitleRegular.Text = "Early Bird Special";
            lblTitleRegular.AutoSize = true;
            lblTitleRegular.Location = new Point(12, 48);
            lblTitleRegular.Font = fntWrite;
            Controls.Add(lblTitleRegular);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            fntWrite = new Font("Brush Script MT", 44.218F);

            e.Graphics.DrawString("Early Bird Special", fntWrite,
                                  Brushes.Black, 12.00F, 208.824F);
        }
    }
}

This would produce:

Drawing Strings - The Style of Text

Most fonts are designed to display their characters standing regularly. If you are planning to use a common font and want to italicize it, to support italic text, the FontStyle enumeration is equipped with a member named Italic. To apply this style to a piece of text, call the above constructor and pass the third argument as FontStyle.Italic. Here is an example:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitle = new Label();

            Font fntWrite = new Font("Verdana", 48.946F, FontStyle.Italic);

            lblTitle.Text = "Geometric Figures";
            lblTitle.AutoSize = true;
            lblTitle.Location = new Point(12, 40);
            lblTitle.Font = fntWrite;
            Controls.Add(lblTitle);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            Font fntWrite = new Font("Verdana", 48.946F, FontStyle.Italic);

            e.Graphics.DrawString("Geometric Figures", fntWrite,
                                  Brushes.Black, 12.00F, 228.824F);
        }
    }
}

This would produce:

Drawing Strings - The Style of Text

If some text exists already, to let you find out whether it is italicized, the Font class is equipped with a read-only property named Italic:

public bool Italic { get; }

This is a Boolean property. Here is an example of accessing it:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitle = new Label();

            Font fntWrite = new Font("Bell MT", 39.238F, FontStyle.Italic);

            lblTitle.Text = "Historical World Events";
            lblTitle.AutoSize = true;
            lblTitle.Location = new Point(12, 40);
            lblTitle.Font = fntWrite;
            Controls.Add(lblTitle);

            bool italicised = fntWrite.Italic == true;

            if(italicised)
            {
                MessageBox.Show("Text on this document is italicized.", "Documentation",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            Font fntWrite = new Font("Bell MT", 39.238F, FontStyle.Italic);

            e.Graphics.DrawString("Historical World Events", fntWrite,
                                  Brushes.Black, 12.00F, 228.824F);
        }
    }
}

This would produce:

An Italicised Text

An Italicised Text

A Bold Text

When you have many text pieces on a document, sometimes you want a certain character or a section to stand out compared to the other letters. You can "fatten" such a letter or section. Such a letter or section is said to be bold. Some fonts are designed with "fat" letters. If you want bold text, you can simply select one of those fonts. Here is an example for a font named Elephant:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        private Font? fntWrite;

        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitleRegular = new Label();

            fntWrite = new Font("Elephant", 34.836F);

            lblTitleRegular.Text = "Day of Celebration";
            lblTitleRegular.AutoSize = true;
            lblTitleRegular.Location = new Point(12, 46);
            lblTitleRegular.Font = fntWrite;
            Controls.Add(lblTitleRegular);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            fntWrite = new Font("Elephant", 34.836F);

            e.Graphics.DrawString("Day of Celebration", fntWrite,
                                  Brushes.Black, 12.00F, 218.824F);
        }
    }
}

This would produce:

Drawing Strings - Striking Out Text - Bold

Of course, not all fonts are designed like. For other fonts, to assist you in making text bold, the FontStyle enumeration is equipped with a member named Bold. Here is an example of applying this effect:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        private Font? fntWrite;

        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitleRegular = new Label();

            fntWrite = new Font("Bell MT", 41.218F, FontStyle.Regular);

            lblTitleRegular.Text = "World Peace/Trade";
            lblTitleRegular.AutoSize = true;
            lblTitleRegular.Location = new Point(12, 20);
            lblTitleRegular.Font = fntWrite;
            Controls.Add(lblTitleRegular);

            Label lblTitleBold = new Label();

            fntWrite = new Font("Bell MT", 41.218F, FontStyle.Bold);

            lblTitleBold.Text = "World Peace/Trade";
            lblTitleBold.AutoSize = true;
            lblTitleBold.Location = new Point(12, 155);
            lblTitleBold.Font = fntWrite;
            Controls.Add(lblTitleBold);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            Font fntWrite = new Font("Bell MT", 41.218F, FontStyle.Bold);

            e.Graphics.DrawString("World Peace/Trade", fntWrite,
                                  Brushes.Black, 12.00F, 288.824F);
        }
    }
}

This would produce:

Drawing Strings - Bold

If you have text that uses a certain font, to let you find out whether that text is bold, the Font class provides a read-only property named Bold:

public bool Bold { get; }

Striking Text Out

You probably already know that if you don't need a certain text piece in your document, you can simply delete that piece. In some cases, you want to keep that text but only put a sign on it. One common way to do this is to draw a line accross that text. To assist you with this, the FontStyle enumeration is equipped with a member named Strikeout. To apply this effect, call the above constructor and pass the third argument as FontStyle.Strikeout. Here is an example:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitle = new Label();

            Font fntWrite = new Font("Californian FB", 47.427F, FontStyle.Strikeout);

            lblTitle.Text = "Binary Classification";
            lblTitle.AutoSize = true;
            lblTitle.Location = new Point(12, 40);
            lblTitle.Font = fntWrite;
            Controls.Add(lblTitle);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            Font fntWrite = new Font("Californian FB", 47.427F, FontStyle.Strikeout);

            e.Graphics.DrawString("Binary Classification", fntWrite,
                                  Brushes.Black, 12.00F, 208.824F);
        }
    }
}

This would produce:

Drawing Strings - Striking Out Text

If you have a piece of text and want to know if it is stroke with a line going throughout, to assist you in getting this information, the Font class is equipped with a read-only property named Strikeout:

public bool Strikeout { get; }

Combining Styles on a Font

You can combine two or more styles to get a fancier effect. To combine the styles, you use the OR bit operator "|" between two FontStyle selection. Here are examples:

private void Exercise_Paint(object sender, PaintEventArgs e)
{
    Graphics grapher = e.Graphics;
    string strDisplay = "Regular Text";
    fntWrite = new Font("Georgia", 28.275F, FontStyle.Regular);

    grapher.DrawString(strDisplay, fntWrite,
                          Brushes.Black, 12.00F, 18.824F);

    strDisplay = "Strikeout and Italic";
    fntWrite = new Font("Georgia", 28.275F, FontStyle.Strikeout | FontStyle.Italic);

    grapher.DrawString(strDisplay, fntWrite,
                          Brushes.Black, 12.00F, 108.824F);

    strDisplay = "Italic, Bold, and Underline";
    fntWrite       = new Font(fntWrite!, FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);

    grapher.DrawString(strDisplay, fntWrite,
                          Brushes.Black, 12.00F, 202.824F);
}

This would produce:

Drawing Strings - Combinations of Styles

A Family of Fonts

Introduction

When you decide to display text on a form or a control, you must specify the font you want to use to draw its text. There are many other decisions you may want to make sure that either your text looks standardized or the letters appear fancy. As it happens, fonts are considered in categories based on their design, the goal of their use, personal preferences, etc. To support the categories of fonts, the .NET Framework provides a sealed class named FontFamily:

public sealed class FontFamily : MarshalByRefObject, IDisposable

The FontFamly class is equipped with three constructors that each provides a different means of initializing a font.

Creating a Font

As seen in our introductions, the primary piece of information you must provide about a font is its name. If you decide to create a font from a font family, to let you specify the font you want, the FontFamily class is equipped with the following constructor:

public FontFamily(string fontName);

This constructor takes as argument the name of the font you want to use. Here is an example of using it:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily family = new FontFamily("Times New Roman");
}

When using this constructor, you should make sure the font you use exists in the computer that will run the application. If you provide a name of a font that doesn't exist in the computer that is running the application, the compiler would throw an ArgumentException exception.

Using a Font

We already know that, to use a font in your application, you can build it from a Font object. As another way, you can identify a font using the FontFamily object and then associate that object with a font. This means that you can first create a font using one of the constructors of the FontFamily class. After creating a FontFamily object, to let you associate it with a font, the Font class provides many options. One option is through the following constructor:

public Font(FontFamily family, float emSize);

Therefore, after creating a FontFamily object, you can pass it as the first argument of the above Font constructor and specify the desired size of text as the second argument. After doing that, you can use the Font object as we have done so far. Here is an example:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitle = new Label();

            FontFamily family = new FontFamily("Colonna MT");
            Font fntWrite = new Font(family, 57.026F);

            lblTitle.Text = "Atlantic Ocean";
            lblTitle.AutoSize = true;
            lblTitle.Location = new Point(12, 40);
            lblTitle.Font = fntWrite;
            Controls.Add(lblTitle);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            FontFamily family = new FontFamily("Colonna MT");
            Font fntWrite = new Font(family, 57.026F);

            e.Graphics.DrawString("Atlantic Ocean", fntWrite,
                                  Brushes.Black, 12.00F, 208.824F);
        }
    }
}

This would produce:

A Family of Fonts - Creating a Font from a Family and Associating it to a Font Object

In the above example, we first declared a FontFamily variable. This is necessary if you are planning to use the same FontFamily object in many code sections. If you are planning to use the font only once, you can create the family font directly in a Font constructor. This can be done as follows:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitle = new Label();

            Font fntWrite = new Font(new FontFamily("Amazone BT"), 52.137F);

            lblTitle.Text = "A Gift is a Debt.";
            lblTitle.AutoSize = true;
            lblTitle.Location = new Point(12, 20);
            lblTitle.Font = fntWrite;
            Controls.Add(lblTitle);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            Font fntWrite = new Font(new FontFamily("Amazone BT"), 52.137F);

            e.Graphics.DrawString("A Gift is a Debt.", fntWrite, Brushes.Black, 12.00F, 228.824F);
        }
    }
}

This would produce:

Drawing Strings

Characteristics of a Font Family

The Name of a Font

To let you identify the family of fonts to which a certain font belongs, the FontFamily class is equipped with a read-only property named Name:

public string Name { get; }

To get the name that a FontFamily object is using, get the value of its Name property.

The Fonts of an Operating System

As mentioned already, every computer or operating system has some fonts installed in it. To give you access to the fonts installed in a computer, the FontFamily class is equipped with a property named Families:

public static System.Drawing.FontFamily[] Families { get; }

Notice that the FontFamily.Families property is static, which means you don't need a variable to access it. Also notice that the property is an array. This allows you to access each one of its items using an index, and you can use a loop (while, do...while, for, or foreach) to access its items. Here is an example:

private void FamilyOfFonts_Load(object sender, EventArgs e)
{
    int counter = 0;

    while(counter < FontFamily.Families.Length)
    {
        lbxFonts.Items.Add(FontFamily.Families[counter].Name);
        counter++;
    }

    txtNumberOfFonts.Text = FontFamily.Families.Length.ToString();
}

This would produce:

The Fonts of an Operating System

The Style of a Font on a Family

In our introduction to fonts, we saw that you can apply some style(s) to a font to enhance the appearance of your text. We reviewed that functionality using only a font. We already know how to store the name of a font in a FontFamily object and pass that font family to a font. After doing that, you may want to apply a style to the font. To support this, the Font class is equipped with the following constructor:

public Font(FontFamily family, float emSize, FontStyle style);

With this constructor, pass a font family as the first argument, the desired text size as the second argument, and a style as the third argument. Here is an example:

namespace DrawingStrings
{
    public partial class Exercise : Form
    {
        Font? fntWrite;
        FontFamily family = new FontFamily("Carmina Blk BT");

        public Exercise()
        {
            InitializeComponent();
        }

        private void Exercise_Load(object sender, EventArgs e)
        {
            Label lblTitleRegular = new Label();

            fntWrite = new Font(family, 41.218F, FontStyle.Regular);

            lblTitleRegular.Text = "Dangerous Lives";
            lblTitleRegular.AutoSize = true;
            lblTitleRegular.Location = new Point(12, 20);
            lblTitleRegular.Font = fntWrite;
            Controls.Add(lblTitleRegular);

            Label lblTitleBold = new Label();

            fntWrite = new Font(family, 41.218F, FontStyle.Underline);

            lblTitleBold.Text = "Dangerous Lives";
            lblTitleBold.AutoSize = true;
            lblTitleBold.Location = new Point(12, 155);
            lblTitleBold.Font = fntWrite;
            Controls.Add(lblTitleBold);
        }

        private void Exercise_Paint(object sender, PaintEventArgs e)
        {
            fntWrite = new Font(family, 41.218F, FontStyle.Underline);

            e.Graphics.DrawString("Dangerous Lives", fntWrite,
                                  Brushes.Black, 12.00F, 301.136F);
        }
    }
}

This would produce:

Drawing Strings

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2010-2024, FunctionX Monday 03 June 2024, 15:36 Next