XAMARIN:跨平台字体族名称

3
我需要为应用程序中的不同标签指定不同的字体族。我需要使用默认字体 (例如 Android 的 Roboto 和 iOS 的 Helvetica) 及其修改版 (例如 Light、Medium 和 Bold)。据我所知,我应该使用 Roboto-Light 和 Helvetica-Light 来获取字体的 Light 版本 (Medium 和 Bold 同理)。 除此之外,我需要在 XAML 中设置字体 (就像 文档中描述的那样),因此最终代码如下:
<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中的结果是出乎意料的。不同标签的字体族并未改变,它们看起来都一样。

Android

相同的代码在iOS上按预期工作

enter image description here

我的问题是:如果按照XAMARIN文档操作无效,那么如何在我的Android应用程序中获取Roboto-Light、Roboto-Medium和Roboto-Bold字体?
4个回答

4

更新:

我第一次没有看到您使用的是API 18/4.3(在模拟器的标题栏中),以为您正在将它们作为旧Android版本的自定义资源加载。由于自4.1以来Roboto是默认字体,因此您可以使用以下字体:

  • sans-serif
  • sans-serif-light
  • sans-serif-condensed
  • sans-serif-thin (4.2+)

原文:

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

Xamarin.Forms for Android目前不支持将字体设置为自定义字体文件,因此需要使用自定义渲染器。在编写Android平台的渲染器时,创建一个Typeface实例,该实例引用已添加到应用程序Assets目录中的自定义字体文件(具有构建操作: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;
        }
    }
}

感谢您注意到自Android 4.1以来,无衬线字体系列实际上使用了Roboto字体。我对您的回答只有一个补充。请注意,与helvetika-bold不同,没有sans-serif-bold(或类似的)字体。为了将字体设置为粗体,我将Label.FontAttributes属性设置为Bold。这满足了我的要求。或者至少在设计师查看应用程序之前是这样的 :) - Pavel Pavlov
这里的第一个答案也证实了这个说法。 - Pavel Pavlov
@SushiHangover - 你上面发布的链接现在显示自定义字体已经可以在Android上使用了。然而,我按照文章中的步骤操作后仍然没有看到我的自定义字体在Android上被使用。你知道自定义字体是否确实还不支持在Android上使用吗? - James Lavery
@JamesLavery Xamarin.Forms现在确实支持Android上的自定义字体,字体命名非常关键:https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/#Android - SushiHangover
@SushiHangover - 谢谢。我已经按照那个指南实现了,但它没有识别出自定义字体。正如你所说,字体的命名(而不是 TTF 文件)非常重要。有些地方存在不匹配,我需要找到原因。 - James Lavery

1
我已经为我所需要的所有视图覆盖了默认的渲染器。
这样,您可以像您打算的那样使用XAML代码:
<Label FontFamily="Arial" Text="Hi there" />

这是一个关于标签(Label)的例子。
[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

2020年更新,Xamarin.Forms 4.5现在支持自定义字体,这些字体嵌入到一个地方(共享UI项目)并通过ExportFont属性导出:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts#use-a-custom-font - iOS、Android和UWP。对于macOS和WPF,您仍然需要按照下面描述的较长路径进行操作。

截至2018年,在Xamairn.Forms中,不需要平台渲染器。步骤如下:

  1. 下载TTF文件,并将它们作为资源与每个平台项目捆绑。
  2. 从Xamarin.Forms共享项目中引用字体,考虑到每个平台的特殊性(用于寻址资源和字体系列的命名约定)。
  3. 将字体应用于Xamarin.Forms控件。

这里有详细的说明,介绍如何在macOS、WPF和Android中添加和应用自定义字体。


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);
        }
    }
}
<Label FontFamily="Roboto-Light.ttf" Text="Hi there" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接