Use custom fonts in xamarin forms

前端 未结 2 366
别那么骄傲
别那么骄傲 2021-01-25 08:08

I want to use custom font in xaml file in xamarinforms on the portable folder(not iOS and android) that get an output for all of this platform.

相关标签:
2条回答
  • 2021-01-25 08:34

    If you don't want do implement this on your own, you can use the CustomFontEffect we added in the CommunityToolkit for Xamarin forms : https://github.com/FormsCommunityToolkit/FormsCommunityToolkit/tree/dev/src

    There is an example project added but overal usage is as follows:

    <Label Text="Comic Sans is tha bomb!">
        <Label.Effects>
            <effects:CustomFontEffect FontPath="ComicSaaaaaans.ttf" FontFamilyName="Comic Sans MS" />
        </Label.Effects>
    </Label>
    
    0 讨论(0)
  • 2021-01-25 08:35
    1. Add fonts into your platforms as explained here.
    2. Create a font style that reflects on your platform like:

          <OnPlatform x:Key="NormalFont" x:TypeArguments="x:String"
                      iOS="Montserrat" Android="Montserrat"
                      WinPhone="Assets/Fonts/Montserrat-Regular.ttf#Montserrat"/>
      
    3. use it with any of below options:

      • Direct using : <Label Text="test" FontFamily="{StaticResource NormalFont}"/>

      • Using With anonymous style (that affects all labels in your app):

        <Style TargetType="Label">
            <Setter Property="FontFamily" Value="{StaticResource NormalFont}"/>
        </Style>
        
      • Use with named Style:

        <Style x:Key="TitleStyle" TargetType="Label">
            <Setter Property="FontFamily" Value="{StaticResource Boldfont}"/>
        </Style>
        
        <Label Text="Options" Style="{StaticResource TitleStyle}"/>
        
    0 讨论(0)
提交回复
热议问题