Embedded Fonts: Custom Fonts in Xamarin.Forms
There are a number of posts about using custom fonts in Xamarin.Forms. However, embedded fonts definitely takes the cake. No more platform-specific handling of fonts and adding font files in three different projects. Just one file and an attribute in your shared code and your font is ready to go. In this post we will see how to use it in Xamarin.Forms 4.5.530 and up.
Life Before Embedded Fonts
As mentioned, there are a couple of posts on using custom fonts already. The most recent one can be found here. In that post, you will see, that depending on the platform, you would need to go through some hoops. Such as adding an entry to the metadata or adding the font file to each target platform project. As well as setting the right build action, finding out the correct font name, and then cross your fingers everything was right the first time.
Written down like this, it doesn’t seem like a lot of work and granted. You only have to do it once (per font), but it can still be a hassle. Especially the part about finding the correct font-family name (or postscript name), involves some trail-and-error. If you have worked with custom fonts before, you already know these embedded fonts will be a big improvement.
How To Use Embedded Fonts
As of Xamarin.Forms 4.5, we will make your life a bit easier yet again. In just three simple steps, we will add a font to use across iOS, Android and UWP.
1. Add the font file (otf or ttf) to your shared project and mark it as embedded resource
Simply add the font file
Samantha.ttf to any folder in your project and set the Build Action to
EmbeddedResource. In this case, find it here.
2. Add ExportFont attribute in your shared project
The most obvious place would be inside your
AssemblyInfo.cs. However, since the attribute will register this on assembly level, you can put this anywhere. The placement of the attribute depends on how visible you want to make it to other/future developers.
Anywhere outside a namespace will work. Just add this, you don’t need to include the subfolders:
3. Consume the new font in your controls
That’s it! You are now ready to consume the font in your app. Through code or XAML, and directly on a control or through a style. Notice that you don’t need to find out the postscript font name anymore. Just use the fonts filename. Which will just work on all platforms!
Note that adding an entry in your
info.plist was not added for iOS. That is also not needed anymore.
To use a more custom name, alternatively, use the Alias property. There you can specify a name to reference this font by. For example:
[assembly: ExportFont("Samantha.ttf", Alias = "MyAwesomeCustomFont")]
Now reference this font by using
Here is an example of using it with a
Label. Where it says
MyAwesomeCustomFont, it could also have said
Samantha when you want to simply use the filename.
<Label Text="Welcome to Xamarin.Forms!" FontFamily="MyAwesomeCustomFont" FontSize="50" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
Under the hood, the font is loaded and cached whenever you start the app. Because of that, be mindful of how many custom fonts you are adding. Although, this is no different than how it works on the native platforms today.
All the information on using custom fonts in Xamarin.Forms can also be found in the docs.
Recap of Embedding Fonts
In closing, the “old” way still works fine. Your code can remain exactly the same and nothing will change. In fact; if you have custom fonts in place right now, just leave it like that unless you have a good reason to change it. If you are starting a new project or want to tweak maintainability, embedded fonts is for you.
We hope this functionality will make the experience when working with fonts much simpler. Needless to say, this also works with icon fonts like Font Awesome, seen in the sample project below.
Want to learn more about what is new in the latest Xamarin.Forms release? Check out the release notes.
Besides fonts there is more you can do to create stunning apps. Check out this earlier post about Snppts for pre-made designs. Maybe you want a set of controls ready to go, like Grial Kit. Now is the time to create beautiful apps with Forms.
Take a look at the code used in this post in this GitHub Repo: https://github.com/jfversluis/EmbeddedFontsSample