Import Custom Font

There are situations especially when branding is a concerns and we need to provide dynamic assets like images with overlay text in a particular font style to make a consistent user experience across the brand.

What if the font, used by brand is not a regular system font.

Here’s a C# solution to import & use a custom Font file.

/// <summary>
/// Reads Font file and return the Font object
/// </summary>
/// <param name="CustomFontFilePath">Path of Font File (TTF File)</param>
/// <param name="CustomFontSize">Font Size</param>
/// <param name="CustomFontStyle">Font Style</param>
/// <returns></returns>
public static Font GetCustomFont(string CustomFontFilePath, float CustomFontSize, FontStyle CustomFontStyle)
{
try
{
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(CustomFontFilePath);
return new Font(pfc.Families[0], CustomFontSize, CustomFontStyle);
}
catch (Exception ex)
{
//Log exception here
return new Font("Arial", CustomFontSize);
}
}

public static Font GetCustomFont(string CustomFontFilePath, float CustomFontSize)
{
return FontHandler.GetCustomFont(CustomFontFilePath, CustomFontSize, FontStyle.Regular);
}

Enjoy!! Make a consistent user experience for your brand.

(Visited 38 times, 1 visits today)