Reputation: 51
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.
Upvotes: 1
Views: 5906
Reputation: 211
You can directly add font file to PCL project and set is as embedded resource. Then declare export font in assembly.cs file like given below
[assembly: ExportFont("customFont.ttf", Alias="CustomFontName")]
Consume it directly in label using alias name
<Label Text="This is custom font examaple"
FontFamily="CustomFontName" FontSize="50"
HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
Hope it helps!
Upvotes: 1
Reputation: 6142
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>
Upvotes: 1
Reputation: 3698
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"/>
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}"/>
Upvotes: 6