2015-09-23 176 views
1

我需要为我的应用中的不同标签指定不同的FontFamily。我需要使用默认字体(例如Roboto for Android和Helvetica for iOS)及其修改(例如Light,Medium,Bold)。据我了解,我应该使用Roboto-Light和Helvetica-Light来获得Light版本的字体(对于Medium和Bold来说也是一样)。 除了这个要求,我需要设置字体在XAML(如described in documentation),所以我结束了这段代码XAMARIN:跨平台FontFamily

<StackLayout BackgroundColor="#F8F8F8" Padding="0, 20, 0, 0"> 
<Label Text="Black" TextColor="#000000" > 
    <Label.FontSize> 
    <OnPlatform x:TypeArguments="x:Double" 
       iOS="17" 
       Android="16" 
       WinPhone="16" /> 
    </Label.FontSize> 
    <Label.FontFamily> 
    <OnPlatform x:TypeArguments="x:String"> 
     <OnPlatform.iOS>Helvetica-Black</OnPlatform.iOS> 
     <OnPlatform.Android>Roboto-Black</OnPlatform.Android> 
     <OnPlatform.WinPhone></OnPlatform.WinPhone> 
    </OnPlatform> 
    </Label.FontFamily> 
</Label> 
<Label Text="Light" TextColor="#000000"> 
    <Label.FontSize> 
    <OnPlatform x:TypeArguments="x:Double" 
       iOS="17" 
       Android="16" 
       WinPhone="16" /> 
    </Label.FontSize> 
    <Label.FontFamily> 
    <OnPlatform x:TypeArguments="x:String"> 
     <OnPlatform.iOS>Helvetica-Light</OnPlatform.iOS> 
     <OnPlatform.Android>Roboto-Light</OnPlatform.Android> 
     <OnPlatform.WinPhone></OnPlatform.WinPhone> 
    </OnPlatform> 
    </Label.FontFamily> 
</Label> 
<Label Text="Medium" TextColor="#000000" > 
    <Label.FontSize> 
    <OnPlatform x:TypeArguments="x:Double" 
       iOS="17" 
       Android="16" 
       WinPhone="16" /> 
    </Label.FontSize> 
    <Label.FontFamily> 
    <OnPlatform x:TypeArguments="x:String"> 
     <OnPlatform.iOS>Helvetica-Medium</OnPlatform.iOS> 
     <OnPlatform.Android>Roboto-Medium</OnPlatform.Android> 
     <OnPlatform.WinPhone></OnPlatform.WinPhone> 
    </OnPlatform> 
    </Label.FontFamily> 
</Label> 
<Label Text="Bold" TextColor="#000000"> 
    <Label.FontSize> 
    <OnPlatform x:TypeArguments="x:Double" 
       iOS="17" 
       Android="16" 
       WinPhone="16" /> 
    </Label.FontSize> 
    <Label.FontFamily> 
    <OnPlatform x:TypeArguments="x:String"> 
     <OnPlatform.iOS>Helvetica-Bold</OnPlatform.iOS> 
     <OnPlatform.Android>Roboto-Bold</OnPlatform.Android> 
     <OnPlatform.WinPhone></OnPlatform.WinPhone> 
    </OnPlatform> 
    </Label.FontFamily> 
</Label> 

然而,结果Android是意想不到的。不同标签的FontFamily不会更改。他们都看起来一样。

Android

iOS中相同的代码工作预期

enter image description here

我的问题是:如何获得的Roboto光强的Roboto介质的Roboto粗体字体在我的Android应用程序中,如果下面的XAMARIN文档不起作用?

回答

4

更新:

我没有看到你使用API​​ 18/4.3的第一次(在你的仿真器的标题栏),以为你装为自定义资产较旧的Android版本。由于的Roboto是因为4.1的默认字体,可以使用它们作为:

  • 无衬线
  • 无衬线光
  • 无衬线冷凝
  • 无衬线薄(4.2 +)

原件:

https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/

Xamarin.Forms for Android目前无法将字体设置为 以自定义字体文件,因此需要自定义渲染器。 在为Android平台编写渲染器时,请创建一个字体 实例,该实例引用已添加到应用程序的Assets目录(使用Build Action:AndroidAsset)的自定义字体文件。

[assembly: ExportRenderer (typeof (MyLabel), typeof (MyLabelRenderer))] 
namespace WorkingWithFonts.Android { 
    public class MyLabelRenderer : LabelRenderer { 
     protected override void OnElementChanged (ElementChangedEventArgs<Label> e) { 
      base.OnElementChanged (e); 
      var label = (TextView)Control; // for example 
      Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "SF Hollywood Hills.ttf"); 
      label.Typeface = font; 
     } 
    } 
} 
+0

感谢您注意到sans-serif字体家族实际上使用了自Android 4.1以来的Roboto字体。 我只有一个除了你的答案。请注意,不像helvetika-bold,没有sans-serif-bold(或类似的)字体。为了将字体设置为粗体,我将Label.FontAttributes属性设置为粗体。这实现了我的要求。或者至少在设计师看看应用程序之前实现它们:) –

+0

第一个答案[here](http://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in- android)也证实了这一说法。 –

+0

@SushiHangover - 您上面张贴的链接现在表示自定义字体现在可在Android上使用。不过,尽管遵循了文章中的步骤,但我并未在Android上看到我的自定义字体。你知道自定义字体实际上是否仍然不支持Android? –

1

我做覆盖,因为我需要它为所有视图的默认渲染。

这样,像你这样的打算,你可以使用XAML代码:

<Label FontFamily="Arial" Text="Hi there" /> 

下面是标签的例子。

[assembly: ExportRenderer (typeof (Label), typeof (MyLabelRenderer))] 
namespace MyApp.Droid 
{ 
    public class MyLabelRenderer : LabelRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
     { 
      base.OnElementChanged(e); 

      if (!String.IsNullOrEmpty(Element.FontFamily)) 
       Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily + ".otf"); 
     } 
    } 
} 

当然,您需要将它映射到您嵌入自定义字体的位置。在我的情况下,Assets/Fonts/Arial.otf

0

请使用以下渲染,加载自定义字体的标签,请确保你把你的字体文件中的字体文件夹中的Android项目的资产文件夹中定义的所有平台

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))] 
namespace MyApp.Droid.Renderer 
{ 
    public class CustomLabelRenderer : LabelRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
     { 
      base.OnElementChanged(e); 
      if (e.NewElement != null) 
       if (!String.IsNullOrEmpty(e.NewElement.FontFamily)) 
        Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily); 
     } 
    } 
} 

XAML相似的路径请确保字体文件在字体文件夹中可用

<Label FontFamily="Roboto-Light.ttf" Text="Hi there" />