Hi,
I'm following the Fonts Tutorial proposed by Xamarin to have a custom downloaded fonts over each plateform. However, I got lot of problems and I also try to find a way to do some things but whitout find how to make it works.
First, take a look at the tutorial and begin at the concerning part. Using a Custom Font
iOS:
It says that we have to add the font.tff into the Resources folder.
Then, we have to do something with the Info.plist, but what? I'm not sure to understand what I have to do.
Androïd:
Xamarin.Forms for Android does not currently expose the ability to set the font to a custom font file, so custom renderers are required.
CustomLabelRenderer.cs
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
System.Diagnostics.Debug.WriteLine(Forms.Context.ApplicationContext.Assets, e.NewElement.StyleId + ".ttf");
if (!string.IsNullOrEmpty(e.NewElement?.StyleId))
{
var font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, e.NewElement.StyleId + ".ttf");
Control.Typeface = font;
}
}
}
According to the documentation, the OnElementChanged()
would be called, however, it doesn't. Debug.WriteLine() doesn't display anything. Why?
WinPhone 8.1:
We have to add the font file to the /Assets/Fonts/
folder in the application project and set the Build Action:Content. Once realized, Xamarin.Forms for Windows Phone can reference a custom font that has been added to the project by following a specific naming standard. However, the Font doesn't take place on the view, the font stays standard, why?
At the moment, after lot of searches, lot of try... Nothing..
I'm trying to add DIN Condensed Bold
font
My XAML layout:
<ContentPage.Content>
<AbsoluteLayout BackgroundColor="#235A5E">
<!-- Menu -->
<AbsoluteLayout x:Name="Menu" BackgroundColor="#2F767B"
AbsoluteLayout.LayoutBounds="0,0,1,0.1"
AbsoluteLayout.LayoutFlags="All">
<Label x:Name="label" Text="Connection" TextColor="White" FontSize="30" VerticalOptions="Center" HorizontalOptions="Center"
AbsoluteLayout.LayoutBounds="0.5, 0, 0.8, 1"
AbsoluteLayout.LayoutFlags="All"/>
<Button x:Name="ConnectionButton" BackgroundColor="Transparent" BorderColor="Transparent"
AbsoluteLayout.LayoutBounds="1, 0.5, 0.2, 1"
AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
</AbsoluteLayout>
</ContentPage.Content>
So the FontFamily property of the Label can be set like that
<Label Text="Hello World !" FontFamily="DIN Condensed Bold"
But, as told in the tutorial, for the WinPhone by example, it has to be done by another way so, I try to change the FontFamily by the C# part.
public partial class MyPage : ContentPage
{
public string FontFamily { get; set; }
public MyPage()
{
InitializeComponent();
label.FontFamily = Device.OnPlatform(
iOS: "DIN Condensed Bold",
Android: "DIN Condensed Bold",
WinPhone: @"Assets\Fonts\DINCondensedBold.ttf#DIN Condensed Bold"
);
}
}
Which doesn't works too ! I also saw something interesting for me about the XAML part. I want (f the FontFamily works one day..) to have a defintion
we makes me able to just edit one field string to set all Label's FontFamily of my page.
<ContentPage.Resources>
<ResourceDictionary>
</ResourceDictionary>
</ContentPage.Resources>
I saw that I might be able to define a FontFamily for all of the Label of MyPage.xaml, how does it works?
I'm also a newbie about MVVM but I tried to bind my public string FontFamily { get; set; }
to the Label defined in the xaml <Label Text="Hello World !" FontFamily="{Binding FontFamily}"/>
but one more time, without success..
I'm totaly stuck... Who can help either to make works the tutorial or explain me each things I mentioned after the tutorial. Thank for help !