Home > Back-end >  Custom Fonts Don't Work when using them as a static resource (Xamarin Forms)
Custom Fonts Don't Work when using them as a static resource (Xamarin Forms)

Time:03-18

I am using xamarin (pretty much explicitly on an Android emulator to run the app). I have imported 2 fonts I want to use in my app. Everything works fine when using the fonts in the specific tags but when I use the font as a part of a static resource, it just uses the default font.

<Label Text="lorem ipsum" FontFamily="CustomFont1" /> //This Works
<Label Text="lorem ipsum" Style="{StaticResource StyleWithFont}" /> //This doesn't work

the second line shows the text with the default font. Not a lot of info on this stuff on the internet :/

EDIT: this is how i have my style in the app.xaml page (Staatliches is the custom font i have imported which works when i consume it like in the first example but not when i consume it through a style)

<Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Button" x:Key="Blue_Btn">
                <Setter Property="BackgroundColor" Value="#030D30" />
                <Setter Property="TextColor" Value="White" />
                <Setter Property="FontSize" Value="20" />
                <Setter Property="Font" Value="Staatliches" />
                <Setter Property="CornerRadius" Value="9" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="FontAttributes" Value="None" />
                <Setter Property="HeightRequest" Value="40" />
            </Style>
        </ResourceDictionary>

CodePudding user response:

I will advise you to read how the styles are defined and how to use them Xamarin Styling.

In summary you can define your styles on a page you need them and the styles are them only accessible from this page, Or you define them globally then you styles are from every page accessible.

Example on same page:

style definition:

    <ContentPage.Resources>
    <ResourceDictionary>
        <Style TargetType="Label" x:Key="LabelWithCustomFont1" >
            <Setter Property="FontFamily" Value="CustomFont1"/>
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>

Consuming the style:

<Label Text="lorem ipsum" Style="{StaticResource LabelWithCustomFont1}" />

Example on global:

Style definition on App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="Label" x:Key="GlobalLabelWithCustomFont1">
            <Setter Property="FontFamily" Value="CustomFont1"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>

You can consume the style from every page.

CodePudding user response:

Okay nevermind, I realized that I used the wrong keyword to reference the font - I wrote Font instead of FontFamily

  • Related