Xamarin.Forms                
            Caratteri personalizzati negli stili
        
        
            
    Ricerca…
Osservazioni
Risorse da guardare:
Accesso a caratteri personalizzati in Syles
Xamarin.Forms fornisce un ottimo meccanismo per lo styling dell'applicazione cross-platform con stili globali.
Nel mondo mobile la tua applicazione deve essere carina e distinguersi dalle altre applicazioni. Uno di questi caratteri è Caratteri personalizzati utilizzati nell'applicazione.
Con il potente supporto di XAML Styling in Xamarin.Forms hai appena creato lo stile di base per tutte le etichette con i tuoi font personalizzati.
Per includere caratteri personalizzati nei tuoi progetti iOS e Android segui la guida in Uso dei caratteri personalizzati su iOS e Android con Xamarin.Form post scritto da Gerald.
Dichiarare lo stile nella sezione delle risorse del file App.xaml. Questo rende tutti gli stili visibili a livello globale.
Da Gerald post sopra dobbiamo usare la proprietà StyleId ma non è una proprietà associabile, quindi per utilizzarla in Style Setter dobbiamo creare la proprietà Attachable per esso:
public static class FontHelper
{
    public static readonly BindableProperty StyleIdProperty =
        BindableProperty.CreateAttached(
            propertyName: nameof(Label.StyleId),
            returnType: typeof(String),
            declaringType: typeof(FontHelper),
            defaultValue: default(String),
            propertyChanged: OnItemTappedChanged);
    public static String GetStyleId(BindableObject bindable) => (String)bindable.GetValue(StyleIdProperty);
    public static void SetStyleId(BindableObject bindable, String value) => bindable.SetValue(StyleIdProperty, value);
    public static void OnItemTappedChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as Element;
        if (control != null)
        {
            control.StyleId = GetStyleId(control);
        }
    }
}
Quindi aggiungi lo stile nella risorsa App.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:h="clr-namespace:My.Helpers"
             x:Class="My.App">
  <Application.Resources>
    <ResourceDictionary>
        <Style x:Key="LabelStyle" TargetType="Label">
            <Setter Property="FontFamily" Value="Metric Bold" />
            <Setter Property="h:FontHelper.StyleId" Value="Metric-Bold" />
        </Style>
    </ResourceDictionary>
  </Application.Resources>
</Application>
In base al post precedente, dobbiamo creare un Renderer personalizzato per Label che erediti dalla piattaforma Android di LabelRenderer.
internal class LabelExRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        if (!String.IsNullOrEmpty(e.NewElement?.StyleId))
        {
            var font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, e.NewElement.StyleId + ".ttf");
            Control.Typeface = font;
        }
    }
}
Per la piattaforma iOS non sono richiesti riproduttori personalizzati.
Ora puoi ottenere lo stile nel markup della pagina:
Per etichetta specifica
<Label Text="Some text" Style={StaticResource LabelStyle} />
Oppure applica lo stile a tutte le etichette sulla pagina creando Stile basato su LabesStyle
<!-- language: xaml -->
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="My.MainPage">
  <ContentPage.Resources>
    <ResourceDictionary>
        <Style TargetType="Label" BasedOn={StaticResource LabelStyle}>
        </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
  <Label Text="Some text" />      
</ContentPage>