Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FontFamily defined in XAML per platform

In my Xamarin Forms project I would like to define the Form family onect per platform and the use that in the application.

So far I've hardcoded the FontFamily per controltype

  <Style x:Key="TahomaBase_Label" TargetType="Label" BaseResourceKey="SubtitleStyle">
    <Setter Property="FontFamily" Value="Tahoma" />
  ...
 </Style>

Is it posible to set Fotnfamily globally in my XAML code preferable itn a OnPlatform tag?

like image 226
Jens Borrisholt Avatar asked Dec 03 '22 14:12

Jens Borrisholt


1 Answers

Define a style in the App.xaml and then reference that style throughout the app. That way you can set the font once in the App.xaml using the OnPlatform tag and never have to worry about OnPlatform in all your other XAML files.

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PlatformFontSample.App">
    <Application.Resources>
        <ResourceDictionary>
            <OnPlatform x:Key="FontFamilyName" x:TypeArguments="x:String" iOS="MarkerFelt-Thin" Android="OpenSans" WinPhone="Segoe UI" />
            <Style x:Key="FontLabel" TargetType="Label">
                <Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

And then:

<Label Text="{Binding Name}" Style="{DynamicResource FontLabel}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="NoWrap"/>
like image 185
Steven Thewissen Avatar answered Dec 20 '22 14:12

Steven Thewissen