Showing posts with label custom fonts. Show all posts

Adding custom fonts in .NET MAUI (Multi-platform App UI) is a powerful way to improve your app’s design and branding, making it stand ...

How to Apply Custom Fonts in .NET MAUI How to Apply Custom Fonts in .NET MAUI

A blog about android developement

custom fonts

Adding custom fonts in .NET MAUI (Multi-platform App UI) is a powerful way to improve your app’s design and branding, making it stand out to users across all platforms, including Android, iOS, Windows, and macOS. In this guide, we’ll go through each step required to integrate custom fonts into your .NET MAUI app effectively, helping you achieve a polished, professional look.


Why Use Custom Fonts in .NET MAUI?

Custom fonts provide numerous benefits:

  • Brand Consistency: Reinforce your brand identity by using fonts that align with your style.
  • Improved Readability: Choose fonts that enhance the readability and aesthetics of your app, improving the user experience.
  • Unique Design: Differentiate your app from others by using custom fonts that reflect the purpose and personality of your app.

Adding custom fonts in .NET MAUI is simple and can significantly impact your app's overall design.


Steps to Add Custom Fonts in .NET MAUI

This tutorial breaks down the process into three essential steps:

  1. 1. Add the Font File to Your .NET MAUI Project
  2. 2. Register the Font in MauiProgram.cs
  3. 3. Use the Custom Font in Your XAML or C# Code

Let’s go through each step in detail.


Step 1: Add the Font File to Your .NET MAUI Project

  • Download the Font: Find a .ttf (TrueType Font) or .otf (OpenType Font) file of your desired font.
  • Place the Font in the Correct Folder: Drag and drop the font file into the Resources/Fonts folder in your .NET MAUI project. This step is crucial because .NET MAUI loads custom fonts directly from this folder.
  • Example: If your font file is named MyCustomFont.ttf, place it in Resources/Fonts.

Step 2: Register the Font in MauiProgram.cs

After adding the font file to your project, the next step is to register it in MauiProgram.cs to ensure .NET MAUI recognizes it.

  • Open MauiProgram.cs in your project.
  • Locate the ConfigureFonts Method: Inside the CreateMauiApp method, find ConfigureFonts and add a new line to register your font file. Here’s how it should look:
    builder.ConfigureFonts(fonts =>
    {
        fonts.AddFont("MyCustomFont.ttf", "MyCustomFontAlias");
    });
    
    • "MyCustomFont.ttf" is the font file name located in Resources/Fonts.
    • "MyCustomFontAlias" is the alias name for the font, which you will use to apply the font in XAML or C#.

Step 3: Use the Custom Font in XAML or C#

With the font registered, it’s now ready to use in your UI.


Using the Font in XAML

To apply the custom font to a control in XAML, use the FontFamily property with the alias you created.

<Label Text="Hello, .NET MAUI!"
       FontFamily="MyCustomFontAlias"
       FontSize="24"
       TextColor="Black"/>

Using the Font in C#

If you prefer to set the font programmatically in C#, you can apply it like this:

Label label = new Label
{
    Text = "Hello, .NET MAUI!",
    FontFamily = "MyCustomFontAlias",
    FontSize = 24,
    TextColor = Colors.Black
};

Tips for Successful Font Integration

  • Check File Names: Ensure that the font file name in MauiProgram.cs matches the actual file name, including case sensitivity.
  • Confirm Licensing: Verify the font’s licensing if you’re using it commercially to avoid copyright issues.
  • Compatibility: Test the font across all supported platforms to make sure it appears correctly on Android, iOS, Windows, and macOS.

Benefits of Using Custom Fonts in .NET MAUI

Custom fonts bring a range of benefits:

  • Consistent Appearance: Once set up, the font will appear consistently across all platforms.
  • Enhanced User Experience: A well-chosen font can make your app more user-friendly and visually appealing.
  • Cross-Platform Design: MAUI makes it easy to use custom fonts without additional platform-specific coding, simplifying cross-platform development.

Wrapping Up

Custom fonts can transform the look and feel of your .NET MAUI app, making it more engaging and unique. By following these steps, you’ll be able to easily add and apply custom fonts across all pages in your app. This customization not only enhances user experience but also boosts your app's professionalism and appeal. With .NET MAUI, you can bring your app's design to life with fonts that make an impression! Start experimenting with different fonts and see how they can elevate your app's style.

In this article, we will learn how to apply custom fonts to Views in Android like Toolbar, MenuItem apart from the views like TextView...

How to custom fonts to Views - Android How to custom fonts to Views - Android

A blog about android developement

custom fonts


In this article, we will learn how to apply custom fonts to Views in Android like Toolbar, MenuItem apart from the views like TextView, EditText.

FontUtils:

FontUtils is a tiny font utility library used to apply custom fonts to Views. It supports the following views.
  • Toolbar
  • NavigationView
  • Menu
  • Submenu
  • Other Views like EditText, TextView, etc.
This library is support the following Languages.
  • Android – Java
  • Android – Kotlin
  • Android

Coding Part:

Steps:

I have split this part into 3 steps as in the following.
Step 1: Creating New Project with Empty Activity.
Step 2: Setting up the Library for Android Views.
Step 3: Applying Custom fonts to Android Views.

Step 1:Creating New Project with Empty Activity

  1. Open Android Studio and Select Create new project.
  2. Name the project as your wish and select Empty activity.
  3. Click “finish” button to create new project in Android Studio.

Step 2:Setting up the Library for Android Views

In this part, we will see how to setup the library for the project.
  1. Open your app level build.gradle file and add the following lines to download the library.
    …
    implementation 'com.ajts.androidmads.fontutils:fontutils:1.0.1'
    …
    
  2. Then Click “Sync Now” to download the library.
  3. We need to use “compile” instead of “implementation” if we have using Android Studio Version below 3.0.

Step 3:Applying Custom fonts to Android Views

In this part, we will see how to apply custom fonts to Views. Here, we have to use “Typeface” and to more about “TypeFace” Click here.
  1. In this article, I am assigning the fonts from Assets folder. To create assets folder, right on the app folder and select New. Then select Folder and click assets folder.
  2. Then Copy and Paste the fonts you want to use with your application.
  3. The following line shows how to initialize the library.
    // Applying Custom Font
    Typeface typeface = Typeface.createFromAsset(getAssets(), "custom_font.ttf");
    // Init Library
    FontUtils fontUtils = new FontUtils();
  4. Then Initialize your Views and apply the fonts to the Views. The following example shows how to apply the fonts to Toolbar of your application.
    // Apply font to Toolbar
    fontUtils.applyFontToToolbar(toolbar, typeface);
  5. You can apply custom fonts to other views like NavigationView, Menu, Submenu, TextView like in following example.
    // Apply font to NavigationView
    fontUtils.applyFontToNavigationView(navigationView, typeface);
    // Apply font to Menu
    fontUtils.applyFontToMenu(menu, typeface);
    // Apply font to Other Views like TextView...
    fontUtils.applyFontToView(textview, typeface);
    fontUtils.applyFontToView(editText, typeface);
    fontUtils.applyFontToView(radioButton, typeface);
    fontUtils.applyFontToView(checkBox, typeface);

For Kotlin

We need to do the same steps mentioned. But we need to include Kotlin support while creating the project. Apply font to Views in Kotlin as show below.
// Applying Custom Font
val typeface = Typeface.createFromAsset(assets, "custom_font.ttf")
// Init Library
val fontUtils = FontUtils()
// Apply font to Toolbar
fontUtils.applyFontToToolbar(toolbar, typeface)
// Apply font to NavigationView
fontUtils.applyFontToNavigationView(navigationView, typeface)
// Apply font to Menu
fontUtils.applyFontToMenu(menu, typeface)
// Apply font to Other Views like TextView...
fontUtils.applyFontToView(textview, typeface)
fontUtils.applyFontToView(editText, typeface)
fontUtils.applyFontToView(radioButton, typeface)
fontUtils.applyFontToView(checkBox, typeface)

For Xamarin.Android

We can apply the same with Xamarin.Android. To know more Click Here.


Download Code

The article is informative do like and star the repo in GitHub. You can download the code here.
Note:
Java and Kotlin Android Support
https://github.com/androidmads/FontUtils
Xamarin.Android Support
https://github.com/Xamarin-Gists/XAFontUtilsLibrary