Kevin
Kevin

Reputation: 59

How can I code-behind use Font Awesome in Xamarin Forms?

I'm having difficulty creating a form with Font Awesome text. Everything works fine when I'm doing it in .xaml file, like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
             x:Name="contentPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myappname.Main">
   <Label Text="&#xf00c;"
          FontFamily="Font Awesome 5 Free-Solid-900.otf#Regular"
          FontSize="40" />
</ContentPage>

The result is:

enter image description here

Assets catalog:

enter image description here

I'm not creating my app for iOS so I haven't added any additional configuration, i.e. configuring ResourceDictionary. This is what I've tried in Main.xaml.cs (I'll show three ways of doing it because I'm not sure how exactly it should work):

protected override async void OnAppearing()
{
    Label newLabel = new Label()
    {
        Text = "&#xf00c;",
        FontFamily = "Font Awesome 5 Free-Solid-900.otf#Regular",
        FontSize = 40
    };

    contentPage.Content = newLabel;
}

protected override async void OnAppearing()
{
    Label newLabel = new Label()
    {
        Text = "&#xf00c;",
        FontFamily = "Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free-Solid-900.otf",
        FontSize = 40
    };

    contentPage.Content = newLabel;
}

protected override async void OnAppearing()
{
    Label newLabel = new Label()
    {
        Text = "&#xf00c;",
        FontFamily = "Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free-Solid-900",
        FontSize = 40
    };

    contentPage.Content = newLabel;
}

The result in every of them is:

enter image description here

What am I doing wrong?

Upvotes: 0

Views: 389

Answers (1)

Nikko Laurenciana
Nikko Laurenciana

Reputation: 178

Try using \u instead of &#x

so it should look like this:

Text = "\uf00c",

Upvotes: 1

Related Questions