Jens Borrisholt
Jens Borrisholt

Reputation: 6402

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?

Upvotes: 4

Views: 7805

Answers (1)

Steven Thewissen
Steven Thewissen

Reputation: 2981

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"/>

Upvotes: 18

Related Questions