【问题标题】:XAMARIN: cross platform FontFamilyXAMARIN:跨平台字体家族
【发布时间】:2015-12-20 08:19:11
【问题描述】:

我需要为我的应用程序中的不同标签指定不同的 FontFamily。我需要使用默认字体(例如 Android 的 Roboto 和 iOS 的 Helvetica)以及它们的修改(例如 Light、Medium、Bold)。据我了解,我应该使用 Roboto-Light 和 Helvetica-Light 来获得字体的 Light 版本(对于 Medium 和 Bold 相同)。 除了这个要求,我还需要在 XAML 中设置字体(比如described in documentation)所以我最终得到了这个代码

<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 中的结果是出乎意料的。不同 Labels 的 FontFamily 没有改变。它们看起来都一样。

iOS 中的相同代码按预期工作

我的问题是:如何在我的 Android 应用程序中获取 Roboto-LightRoboto-MediumRoboto-Bold 字体(如果以下) XAMARIN 文档不起作用?

【问题讨论】:

    标签: android fonts xamarin


    【解决方案1】:

    更新:

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

    • 无衬线
    • 无衬线灯
    • 无衬线压缩
    • sans-serif-thin (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;
            }
        }
    }
    

    【讨论】:

    • 感谢您注意到自 Android 4.1 以来 sans-serif 字体系列实际上使用了 Roboto 字体。我对你的回答只有一个补充。请注意,与 helvetika-bold 不同,没有 sans-serif-bold(或类似的)字体。为了将字体设置为粗体,我将 Label.FontAttributes 属性设置为粗体。这达到了我的要求。或者至少在设计师查看应用程序之前实现它们:)
    • 第一个回答here也印证了这一说法。
    • @SushiHangover - 您在上面发布的链接现在表明自定义字体现在可以在 Android 上使用。但是,尽管按照文章中的步骤进行操作,但我没有看到我的自定义字体在 Android 上使用。您知道 Android 是否实际上仍然不支持自定义字体吗?
    • @JamesLavery Xamarin.Forms 现在确实支持 Android 上的自定义字体,字体命名至关重要:developer.xamarin.com/guides/xamarin-forms/user-interface/text/…
    • @SushiHangover - 谢谢。我已经根据该指南实施,但它没有选择自定义字体。正如您所说,字体的命名(不是 TTF 文件)至关重要。某处存在不匹配,我需要查明原因。
    【解决方案2】:

    我确实为我需要的所有视图覆盖了默认渲染器。

    这样您就可以按照您的意图使用 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.

    【讨论】:

      【解决方案3】:

      请使用下面的渲染,在标签中加载自定义字体,请确保您将字体文件放在 android 项目的 assets 文件夹中的 fonts 文件夹中,以便为所有平台定义类似的路径

      [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" />
      

      【讨论】:

        【解决方案4】:

        2020 年更新,Xamarin.Forms 4.5 现在支持嵌入在一个位置(共享 UI 项目)并通过 ExportFont 属性导出的自定义字体https://docs.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 控件。

        Here's detailed description 了解如何在 macOS、WPF 和 Android 中添加和应用自定义字体。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多