Bitmap madness
One of the most tedious parts of mobile development, particularly if you are a one-person team, is creating graphic assets.
If you are using images for icons in your UI then you will know the pain of creating different resolution images to support the different target devices of Android and iOS.
A modern icon font can help relieve some of that pain, particularly if you need icons representing common objects or actions – like you would use for a typical menu system, for instance.
In this example we’ll use FontAwesome to provide us with a huge selection of icons that we can use at any size or colour to brighten up our app.
Icon fonts and a custom renderer to the rescue
Including an icon font in your Xamarin Forms app is pretty straightforward, both on Android and iOS.
We’ll start by including the font file in the platform apps, then we’ll extend the Label control to be a FontAwesomeLabel. Thats about it for iOS. On Android we’ll need a small custom renderer to tell it the font name, but it’s not much work.
Including the font
Firstly, download and include the font file in the platform projects. In the Android project the file is under Assets/Fonts with a build action of AndroidAsset. In the iOS project, the file goes under Resources/Fonts with a build action of BundleResource. Note that the Fonts folder is just personal preference.


You also need to add an entry to the iOS info.plist:
<key>UIAppFonts</key> <array> <string>Fonts/FontAwesome.otf</string> </array>
Extend the Label control
Back in the PCL, add a new class called FontAwesomeLabel and extend the Label control with the following:
using Xamarin.Forms; namespace FontIconApp.UserControls { public class FontAwesomeLabel : Label { public static readonly string FontAwesomeName = "FontAwesome"; //Parameterless constructor for XAML public FontAwesomeLabel() { FontFamily = FontAwesomeName; } public FontAwesomeLabel(string fontAwesomeLabel = null) { FontFamily = FontAwesomeName; Text = fontAwesomeLabel; } } // https://github.com/neilkennedy/FontAwesome.Xamarin/blob/master/FontAwesome.Xamarin/FontAwesome.cs // For a huge list of icon codes public static class Icon { public static readonly string FAGlass = "\uf000"; public static readonly string FAMusic = "\uf001"; public static readonly string FASearch = "\uf002"; public static readonly string FAEnvelopeO = "\uf003"; } }
I’ve only included 4 icon names here, but you can see a much bigger selection in the example project.
Use it in XAML
Use the new control in your XAML like so:
<userControls:FontAwesomeLabel Text="{x:Static userControls:Icon.FAMusic}" HorizontalOptions="Center" VerticalOptions="Center" FontSize="18" TextColor="CornflowerBlue" />
Don’t forget to include the relevant namespaces in the header of your XAML.
For iOS that’s all you need. On Android there’s just one more step. You’ll need to add a custom renderer to tell Android the name of the font with it’s full path and extension (e.g. “Fonts/FontAwesome.otf”).
In the Android platform project, add a custom renderer like the one below. I like to put all the renderers into a CustomRenderers folder and namespace.
using Android.Graphics; using FontIconApp.Droid.CustomRenderers; using FontIconApp.UserControls; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(FontAwesomeLabel), typeof(FontAwesomeLabelRenderer))] namespace FontIconApp.Droid.CustomRenderers { public class FontAwesomeLabelRenderer : LabelRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); if (e.OldElement == null) { Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + FontAwesomeLabel.FontAwesomeName + ".otf"); } } } }
Wrap-up
Icon Fonts are a simple way to add some polish to your Xamarin Forms apps without the work of creating multiple graphical assets.
You can download the example code at the XamarinInsider repo.
Hello. My code doesnt work. Can you help me?
In Android doesnt appear the icon. In UWP just apear rectangles.
LikeLike
Hi Andrea, are you getting any exceptions thrown? Also take a look at the example project and see if there is much different in your code.
LikeLike
Now works here! Tks!!!!
LikeLike
Hello! Doesnt works here. Can you help me?
public partial class PesquisaPage : ContentPage
{
private PesquisaViewModel ViewModel => BindingContext as PesquisaViewModel;
public PesquisaPage(Janela janela)
{
InitializeComponent();
BindingContext = new PesquisaViewModel(janela);
ToolbarItems.Add(new ToolbarItem(Control.Icon.FASquareCheck.ToString(), null, () => {}));
}
LikeLike
Hi Andrea, I haven’t done that myself, but this thread might point you in the right direction.
https://forums.xamarin.com/discussion/31745/is-it-possible-to-use-a-custom-font-for-toolbaritems
It will almost certainly involve some more custom renderer work.
LikeLike
Excellent, Thanks!
LikeLike
Hi Nigel
What is the process for UWP, do I need to make a custom renderer or no, thanks
LikeLike
Hi,
I haven’t done much UWP myself but you might want to check out this https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.fonticon
Might start you off in the right direction.
LikeLike
Thanks for sharing.
Is it possible to bind the icon to a property in the view model for example?
Let’s say
2
3
4
5
LikeLike
Hi,
Yeah you can definitely bind the value to a view model. The text property is a string, so you can bind to that as normal. I have found that you need to use the Unicode values from here http://fontawesome.io/3.2.1/cheatsheet/ if you want to either use the values directly in your XAML or bind to a string property.
Hope that helps.
LikeLike
Any comments about how using FontAwesome in Gorilla Player?
LikeLike
Hi. I don’t have a lot of experience with gorilla player, I’m afraid 😦
LikeLike
iOS renderer for font ?
LikeLike
Hi, you won’t need a custom renderer for iOS.
LikeLike