TheDeveloper
TheDeveloper

Reputation: 1217

Xamarin forms: Resource dictionary - Set font family for label, textview and Buttons

I want to set font information in resource dictionary which is used by labels, buttons, textviews.

<OnPlatform x:TypeArguments="x:String" x:Key="HelveticaNeue">
                <On Platform="iOS">HelveticaNeue</On>
                <On Platform="Android">HelveticaNeue#HelveticaNeue</On>
            </OnPlatform>

            <Style x:Key="style1" TargetType="Font">
                <Setter Property="FontFamily" Value="{StaticResource HelveticaNeue}" />
                <Setter Property="FontSize" Value="23" />
            </Style>

I tried above code, but its failing because in the type Font the FontFamily set property is private. For the key style1, if I set the target type as label or button it works. Is there a way I can set font information (which includes font family and size) in resource dictionary at once which can be used by label, button and textviews without duplicating the same type for different target type ?

Upvotes: 0

Views: 2342

Answers (1)

EvZ
EvZ

Reputation: 12169

Usually I have 3 different resources:

  • Font
  • TextSize
  • Style per control

Example:

<OnPlatform
    x:Key="MediumFontFamily"
    x:TypeArguments="x:String"
    Android="sans-serif-medium"
    iOS="HelveticaNeue-Medium" />

<x:Double x:Key="TitleFontSize">20</x:Double>

<Style x:Key="TitleLabel" TargetType="Label">
    <Setter Property="TextColor" Value="{StaticResource HeaderTextColor}" />
    <Setter Property="FontFamily" Value="{StaticResource MediumFontFamily}" />
    <Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
</Style>

This way there should be no duplication, as you are simply reusing resources.

Upvotes: 2

Related Questions