Using Custom Fonts on a Label on Winforms

Using custom fonts on a Label on Winforms

Embed the font as a resource (or just include it in the bin directory), and then use the PrivateFontCollection to load the font (See the AddFontFile and AddMemoryFont functions). You then use the font normally like it was installed on the machine.

The PrivateFontCollection class allows
applications to install a private
version of an existing font without
the requirement to replace the system
version of the font. For example, GDI+
can create a private version of the
Arial font in addition to the Arial
font that the system uses.
PrivateFontCollection can also be used
to install fonts that do not exist in
the operating system.

Source

Using custom fonts in my winform labels

Here is the extract (using PrivateFontCollection):

Dim pfc As New PrivateFontCollection()
pfc.AddFontFile("C:\Path To\PALETX3.ttf")
label1.Font = New Font(pfc.Families(0), 16, FontStyle.Regular)

Converted from here: Load And Use Custom Font Without Installing It.

Also check this: Embedding/deploying custom font in .NET app

Change font of a label with custom font

Use the PrivateFontCollection to load the font (See the AddFontFile and AddMemoryFont). You then use the font normally for label control.

PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile("C:\\Path To\\YourFont.ttf");
label1.Font = new Font(pfc.Families[0], 16, FontStyle.Regular);

C# How to change font of a label

Font.Name, Font.XYZProperty, etc are readonly as Font is an immutable object, so you need to specify a new Font object to replace it:

mainForm.lblName.Font = new Font("Arial", mainForm.lblName.Font.Size);

Check the constructor of the Font class for further options.

Looking for the optimal way to use custom font definitions in WinForms application

Simplest way is to bind the font property in the Properties Window to an application setting, which will end up in the app.config file for your application.



Related Topics



Leave a reply



Submit