2016-10-06 1 views
0

xamarinフォームアプリケーション全体のカスタムフォントをapp.xamlのスタイルを使用して設定しようとしています。しかし、私は同じものに対して未処理の例外を取得しています。xamarinfフォームアプリケーションのカスタムフォントを設定する方法

<Label Style="{StaticResource labelFont}"></Label> 

このため、任意の解決策を次のように

<OnPlatform x:Key="AppFontFamily" x:TypeArguments="x:String" 
     Android="customfont.otf#CustomFont-Regular"> 
</OnPlatform> 

<Style x:Key="labelFont" TargetType="Label"> 
     <SetterProperty Property="FontFamily" Value="{StaticResource AppFontFamily}"></SetterProperty> 
     </Style> 

私のコンテンツページにスタイルを使用していますか?

+0

ドキュメントを読んだことがありますか? https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/#Using_a_Custom_Font – Jason

+0

@Jason、はい同じことを参照しながら作業していますが、アプリケーション全体に共通のフォントを追加する方法は、私はフォントファイルを意味する – user3165999

答えて

0

フォントファイルを使用してすべてのラベルのフォントを設定する場合は、カスタムレンダラーを作成する必要があります。そこでは、オーバーライドは次のようにOnElementChanged:

protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
    { 
     base.OnElementChanged(e); 
     try 
     { 
      Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets, path); 
      Control.Typeface = font; 
     } 
     catch(Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine("Unable to make typeface:" + ex); 
     } 
    } 

しかし、あなたはLabelから継承(PCLで)カスタムラベルクラスを作成し、それに追加する必要があるすべての単一のラベルのためのカスタムフォントを設定できるようにしたい場合フォントプロパティ(正確には、フォントファイルのパスを渡すために使用するプロパティ)です。

public static readonly BindableProperty CustomFontProperty = 
     BindableProperty.Create(
      "CustomFont", 
      typeof(string), 
      typeof(CustomLabel), 
      default(string)); 

    public string CustomFont 
    { 
     get { return (string)GetValue(CustomFontProperty); } 
     set { SetValue(CustomFontProperty, value); } 
    } 

次に、あなたのレンダラであなたは、このように、このプロパティを読み取ることができます:

var label = Element as CustomLabel; 
string path = label.CustomFont; 

は、Androidでのパスが区切り、ないとして「/」を使用することに注意してください「」フォームのように。

関連する問題