Simple way to use icon fonts in Xamarin Forms projects

Almost all apps use some kind of graphic icons to make the UI more attractive and easy to use. Here I will demonstrate a quick way of adding icon fonts in Xamarin applications.

There are plenty of icon sets available as web fonts for free, e.g. FontAwesome, Material Icons, Icofont. But, as mobile devs, we should always be concerned about the app size. For this example, I will use a handy service to create a custom font that only contains the icons I need: Fontello.
To create a custom font:
  1. Select icons that you’re interested in or upload an SVG;
  2. Write down the icons codes, we’ll need them later;
  3. Set a font name and hit the download button.

OK, now we have everything ready to add these icons to the app. Open your Xamarin Forms project in Visual Studio and let’s start with adding the .ttf file to the native projects.

Android

Add the file to the Assets folder and make sure that the Build Action is AndroidAsset (should be set by default).

iOS

Add the file to the Resources folder, make sure that the Build Action is BundleResource (again, should be set by default).
In iOS projects, we also need to list custom fonts in the Info.plist file. There are two ways to do that: double-click on the Info.plist file, select the Source tab and add a ‘Fonts provided by application‘ record (don’t forget to replace customfont.ttf with your font name):
Or if you prefer to edit the Info.plist in a text editor, just add the following lines before the closing </dict>  tag:
<key>UIAppFonts</key>
<array>
    <string>customfont.ttf</string>
</array>

Xamarin Forms Project

At this point, we have everything ready to use the icons in the app. Let’s add a new item to the ResourceDictionary in the App.xaml to handle platform specific differences:

<OnPlatform x:Key="CustomFont" x:TypeArguments="x:String">
    <On Platform="Android" Value="customfont.ttf#fontello" />
    <On Platform="iOS" Value="customfont" />
</OnPlatform>

And finally the icons. Add a label to a page, set the FontFamily and the Text properties:

<Label Text="&xe803;" x:Name="Heart" FontFamily="{StaticResource CustomFont}" FontSize="30" TextColor="White"/>

Notice that we’re using the codes that we wrote down from the Fontello service here. To make the XAML code a bit cleaner we can move all properties to a style and set only the icon code (Text property) in the label. Or even create separate styles for all frequently used icons:

<Style x:Key="Icons" TargetType="Label">
    <Setter Property="FontFamily" Value="{StaticResource CustomFont}" />
    <Setter Property="FontSize" Value="30" />
    <Setter Property="TextColor" Value="White" />
    <Setter Property="WidthRequest" Value="40" />
</Style>
<Style x:Key="LikeIcon" BasedOn="{StaticResource Icons}" TargetType="Label">
    <Setter Property="Text" Value="&#xe801 ;"/>
</Style>
<Label Text="" Style="{StaticResource Icons}" />
<Label Style="{StaticResource LikeIcon}" />

It’s also possible to update the text programmatically from the code-behind or via bindings from the view model. E.g. when you want to show an arrow indicating the sort order:

public string SortIconFullName => _sortBy == "fullname" ? (_sortAsc ? "\uE5C7" : "\uE5C5") : string.Empty;
public string SortIconEmail => _sortBy == "email" ? (_sortAsc ? "\uE5C7" : "\uE5C5") : string.Empty;

And finally a nice bonus — since this is just a label with some text (or any other control with a text), you can easily change the color, size or even add an animation. You can find a sample project here: https://github.com/vecalion/Xamarin.IconFontDemo
Happy coding!

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Adding a New Android Permission in a Xamarin App

As you know, Android is always adding features in its new versions and now the Post Notifications permission has been introduced from Android 13. In this blog, we are going to explore how to request it in our Xamarin.Android projects. Sampl

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.