Fonts - James Montemagno https://staging.montemagno.com/tag/fonts/ https://staging.montemagno.com/content/images/2017/08/MonogramIcon_Orange_NoBuffer.png Fonts - James Montemagno https://staging.montemagno.com/tag/fonts/ Hugo -- gohugo.io Tue, 13 Oct 2026 23:21:57 GMT 60 Xamarin.Forms Custom Fonts Simplified & Everywhere https://staging.montemagno.com/xamarin-forms-custom-fonts-everywhere/ 5eade503e04bb2003806f8da Sat, 02 May 2020 21:56:10 GMT It was just about 1 year ago when I wrote about using font icons for images inside of Xamarin.Forms apps. Well time has flown by and now more than ever is the time to go all in on custom fonts and font icons because with Xamarin.Forms 4.6 everything got so much easier!

Custom fonts in Xamarin.Forms Shell
Custom fonts in Xamarin.Forms Shell

See my full walkthrough:

So, let's start off with the basics when it comes to fonts. You usually think about fonts as just a fancy way to display text. However, with resources such as Font Awesome you can use fonts as icons instead of images. Font Awesome provides a plethora of free font icons and the actual files are small at about 400KB for all 3 free that you can download from this link here. You can put them in Tabs, Labels, and Images, and they are awesome!

Previously there was a lot of setup required, but with Xamarin.Forms 4.6 embedded fonts you can now drag them into your .NET Standard project in Visual Studio and mark them as an EmbeddedResource:

Add fonts and mark as EmbeddedResource
Add fonts and mark as EmbeddedResource

Then all you need to do is head over to your AssemblyInfo.cs or App.xaml.cs and add this new fancy export code. You can even give the font an Alias to easily identify it later.

[assembly: ExportFont("CustomFont.ttf", Alias = "AnimalCrossing")]
[assembly: ExportFont("fa-regular-400.ttf", Alias = "FA-R")]
[assembly: ExportFont("fa-solid-900.ttf", Alias = "FA-S")]
[assembly: ExportFont("fa-brands-400.ttf", Alias = "FA-B")]

When you head over to the font awesome website each icon will have a specific unicode associated with it:

Coffee icon from font awesome with unicode f0f4
Coffee icon from font awesome with unicode f0f4

We can now put these in our Application Resources that we can call on later:

<x:String x:Key="IconCoffee">&#xf0f4;</x:String>

Now, we can use it in our fancy Shell Tabs (or Image) like you saw earlier using FontImageSource:

 <Tab Title="Profile" Route="profile">
     <Tab.Icon>
     	<FontImageSource Glyph="{StaticResource IconCoffee}" FontFamily="FA-S" />
     </Tab.Icon>
 	<ShellContent ContentTemplate="{DataTemplate view:ProfilePage}"/>
 </Tab>

We can also use them in a Label:

<Label
    FontFamily="FA-S"
    Style="{StaticResource SocialLabel}"
    Text="{StaticResource IconCoffee}" />
Custom fonts as labels!
Custom fonts as labels!

If you are referencing your Icons from C# you can generate a full schema from the font by using this awesome site IconFont2Code. Or if you want a pre-generated on checkout Matthew Robin's GitHub fa2cs project that has a pre-made file to do this. Once you add the file you can then have access to every icon!

You can use an icon in XAML by:

  • Adding a namespace reference to FontAwesome: xmlns:fontAwesome="clr-namespace:FontAwesome";
  • Referencing a icon using x:Static: <Label Text="{x:Static fontAwesome:FontAwesomeIcons.Alicorn}"/>
  • or in code submitButton.Text = FontAwesome.FontAwesomeIcons.Check;

Now, go off and use those custom fonts! Want more, read through the docs.

]]>
Using Font Icons in Xamarin.Forms: Goodbye Images, Hello Fonts! https://staging.montemagno.com/using-font-icons-in-xamarin-forms-goodbye-images-hello-fonts/ 5ca1b4d7b2f3d500bff4c999 Thu, 11 Apr 2019 21:47:00 GMT

Updated: See the newest way of importing fonts with Xamarin.Forms 4.6 on my latest blog post.

My full walkthrough:

My entire mobile development career I have gone all in on using device specific images and icons. These are properly scaled and sized images that fit @1x, @2x, @3x, hdpi, xhdpi, xxhdi, and so on, which are great because they are super optimized for each platform. The reason I do this was because it gives very consistent look and feel to an app across all of the different form factors of the devices the app may run on. It also happens to be the official way that Google, Apple, and Microsoft tell you to do it and they even give you tools like the Asset Studio to help you generate them. However, my eyes have been opened and I am all in on FONTS! Yes... FONTS! I bet you thought I was going to say SVG, but NO, FONTS!

fonts

I mean look at all of those beautiful icons right there. I bet you thought they were images... nope those are being generated from a font! The great part about fonts like this is that they bundle tons of icons into a single file and it is really easy to access them with a specific code. additionally, since they are a font they should scale properly across different form factors :)

And up until a few weeks ago, I had no idea that Xamarin.Forms had great built in support for them. So, let's take a look at how to integrate them.

Import the Font (NEW)

In recent versions of Xamarin.Forms you can now easily add your Fonts into your .NET Standard library. Simply mark them as EmbeddedResource as the content type in properties.

Then inside of any file (I like the AssemblyInfo.cs), you can add this export:

[assembly: ExportFont("FASolid.otf", Alias = "FA")]

Here, I am using the Font Awesome font and I can access it directly with the font family name of "FA" that I specified with the Alias.

In the code samples below I show the old way of setting the FontFamily, however if you use this method you would just do FontFamily = "FA".

Import the Font (OLD)

While I was working on a Xamarin.Forms app I got a great pull request that integrated Material Design Icons into the app. It was my first real experience seeing custom fonts to remove a bunch of images that were the same on each platform. All you need to do is grab the ttf file and add it to the correct location.

  • Android: Add it to the Assets folder and ensure it is marked as an AndroidAsset
  • iOS: Add the font file with Build Action: BundleResource, and update the Info.plist file (Fonts provided by application, or UIAppFonts, key).
  • UWP: Add the file to /Assets/Fonts/ and make sure it is marked as Content

Access Font Family (OLD)

To access the fonts directly it is a bit quirky. You need to specify the correct value for each platform as to where the font lives. Since they are different on each platform and we don't want to write a bunch of code every time, we can simply put an OnPlatform key in our application resources:

<OnPlatform x:Key="MaterialFontFamily" x:TypeArguments="x:String">
    <On Platform="iOS" Value="Material Design Icons" />
    <On Platform="Android" Value="materialdesignicons-webfont.ttf#Material Design Icons" />
    <On Platform="UWP" Value="Assets/Fonts/materialdesignicons-webfont.ttf#Material Design Icons" />
</OnPlatform>

Turn Font to Code

Now, we need to access these fonts! You will need the specific codes for each icon in the font. Luckily, Andrei wrote an awesome tool to convert a font file to code called IconFont2Code:

FontToCode

Also, checkou this awesome GitHub Repo where you can download full helper files for Font Awesome and Material Icons.

Text or FontImageSource

What is great now is that we can set any control that has a FontFamily associated with it and simply set the Text property.

<Label FontFamily="{StaticResource MaterialFontFamily}" Text="{Binding Car"}/>

If you want to use the font for an Image, the new FontImageSource can be used:

<FontImageSource
            FontFamily="{DynamicResource MaterialFontFamily}"
            Glyph="{Binding Car}"
            Size="44"
            Color="{StaticResource PrimaryColor}" />

To use the font icons you can do it via a binding to the code that was generated like I did above, or to an escaped string in XAML. Let's look at some examples.

Access from Bindings

One thing that I think is really cool is that since you can use these fonts on any control that has a text property, it means you can use it on a Button!

On my Hanselman.Forms app we have a list of social links that we want to display as icons. Here is our source:

SocialItems = new List<SocialItem>
{
    new SocialItem
    {
        Icon = IconConstants.TwitterCircle, // "\uf547"
        Url = "https://www.twitter.com/shanselman"
    },
    new SocialItem
    {
        Icon = IconConstants.FacebookBox, // "\uf20d"
        Url = "https://www.facebook.com/shanselman"
    },
    new SocialItem
    {
        Icon = IconConstants.Instagram, //"\uf2fe"
        Url = "https://www.instagram.com/shanselman"
    }
};

In our XAML it couldn't be easier to data bind to it:

 <StackLayout
    BindableLayout.ItemsSource="{Binding SocialItems}"
    HorizontalOptions="CenterAndExpand"
    Orientation="Horizontal"
    Spacing="5">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Button
                Command="{Binding OpenUrlCommand}"
                Style="{StaticResource MaterialButton}"
                Text="{Binding Icon}" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Social

Access Directly in XAML

When you want to directly access one of the fonts from XAML you will need to escape the key. So, if we were to convert the car from C# to escaped string:

public const string Car = "\uf10b";

Turns to:

<x:String x:Key="Play">&#xf10b;</x:String>

Then, if we wanted to use it on an Image the code would reference this key:

<Image
    Grid.Column="2"
    HeightRequest="44"
    HorizontalOptions="Center"
    VerticalOptions="Center"
    WidthRequest="44">
    <Image.Source>
        <FontImageSource
            FontFamily="{DynamicResource MaterialFontFamily}"
            Glyph="{StaticResource Play}"
            Size="44"
            Color="{StaticResource PrimaryColor}" />
    </Image.Source>
</Image>

Icons

When Not To Use Fonts

In this instance I am using fonts that are icons inside of the application that are reasonable to look and feel exactly the same across every platform. My recommendation as always is to use the platform look and feel for icons in toolbars and tabs. This will ensure your users have the best experience. Also, be aware that they will add some size to your app (material design icons was around 500KB!). Beyond that I say go for it :)

Read More

Checkout the Hanselman.Forms app on GitHub and see the work in progress with the new icons! Also, be sure to read through the great Font documentation for Xamarin.Forms

Wealthfront: Your cash can do more. Sign up with my invite link to get $5,000 managed for free
]]>
Beautiful Android Compat Roboto Fonts in Xamarin.Android https://staging.montemagno.com/beautiful-android-compat-roboto-fonts-in/ 598df8f2c121b60019bf3048 Fri, 20 Sep 2013 18:59:07 GMT In android 4.1 and 4.2 Google did something amazing, which was add the roboto fonts as a default font family that you can switch between. It is as easy as setting “sans-serif”, “sans-serif-light”, “sans-serif-condensed”, and “sans-serif-thin” as the android:FontFamily.

However the obvious issue here is that this is only available in 4.1+ (with thin only being available in 4.2). So if you want to target older platforms you have to do some custom themes, or different layouts and that is no fun for anyone. These fonts are beautiful and everyone should be able to use them. It is also completely possible since you are able to load custom fonts. In Xamarin.Android it is extremely simple. All you have to do is add your fonts to you asset folder and then:

Typeface.CreateFromAsset(context.Assets, "fonts/Roboto-LightItalic.ttf");

However this is going to be no fun if you have to set it in the code behind for each and every text view. We want to set it in the axml! To do this we are going to have a custom TextView (we will call it RobotoTextView) and it will have a custom attribute (we will call it typeface).

The RobotoTextView will check the custom attribute for the typeface attribute (such as roboto_thin or roboto_light) and then load up the correct font to display. It will also do some caching as well so we don’t load them up tons of times.

After that it is as simple as putting some stuff in you axml. First is to add an xmlns definition. I like to use res-auto and let it figure it out:

xmlns:local="http://schemas.android.com/apk/res-auto"

Then just a com.refractored.controls.RobotoTextView instead of TextView and set this property:

local:typeface="roboto_thin_italic"

Now you are all set! I have setup a full github repo with source code and example!

Your result will look like this:

]]>