Embed the Custom Fonts in a Xamarin Forms Application

Though the cross-platform mobile app development environment Xamarin.Forms introduced a lot of amazing features for the creation of a multi-functioning business empowering mobile applications, nevertheless, it’s adding an embedded font feature is considered as its sweet cake.

In order to add an embedded font in your application with this Xamarin.Forms platform, all you need to edit one file with an attribute in the spread code of your project and you are read are done with the integration of embedded font in your project. In this series, in addition, to add an entry to the metadata and to add the font file in the project along with the setting of a correct build action, accurate font name, etc. earlier when this easy to implement, embedded font feature was not in the existence it was a bit hassle worthy task to add a different font style in a Xamatin app. Especially finding a correct font family, script or font name makes the developers stuck in some major errors. Being a Xamarin developer, you can better know this fact how difficult it was previously and what an amazing improvement the introduction of embedded fonts.

Follow the simple steps to implement such embedded font in your Xamarin.Forms project:

The 4.5 version of Xamarin.Forms have makes the developer’s life a lot easier once again as in just three simple steps now you can add a lavish font style that will deliver a similar user experience on every mobile OS.

  1. Sum the font (otf and ttf) file in tour Xamarin project and sign it as an embedded resource in your project. To implement the functionality simply add the font file in your project folder and set the build action to EmbeddedResource.
  1. Now in the second step, sum your project with the ExportFont attribute. For this use the files as App.xaml.cs or AssemblyInfo.cs. Well, the attributes are registered on this as an assembly level which you can put anywhere in the font file of your project. Just remember a single thing that wherever you are going to put the code, that place should be outside of the namespace. Use this code:

[assemly: ExportFont(“filename.ttf”)]

  1. Access the controls of the new code. That simply means you are now all set to consume the font in your Xamarin.Forms application, via the code or XAML and directly on a control on the font style. Make a note that, here you are not required to search a postscript font name, s just the use of fonts filename will work well on every platform.

Remember  that adding an entry in yourproject file as  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.

For example: [assembly: ExportFont(“Samantha.ttf”, Alias = “MyAwesomeCustomFont”)].

Now reference this font by using MyAwesomeCustomFont.

Here is an code  example of using it with a Label.

<Label Text=”Welcome to Xamarin.Forms!” FontFamily=”Samantha” FontSize=”50″ HorizontalOptions=”Center” VerticalOptions=”CenterAndExpand” />

Conclusion: We hope this post will make the experience when working with fonts much simpler. Needless to say, this also works with icon fonts like Font Awesome, etc. Incase of any other queries, feel free to ask us at he Hire Xamarin Developers.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments