【问题标题】:Xamarin forms: Custom Fonts in UWP and Windows 8.1Xamarin 表单:UWP 和 Windows 8.1 中的自定义字体
【发布时间】:2017-06-01 18:00:08
【问题描述】:

我正在开发一个使用 font.otf 文件的应用程序。我的应用程序将在 android、ios、windows 10 和 windows 8.1 上运行。我需要为我的标签创建样式以设置字体系列。 对于 android 和 ios,我引用了这个 link

我在这样的 xaml 页面中尝试过-

<Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
            <OnPlatform.iOS></OnPlatform.iOS>
            <OnPlatform.Android>Lobster-Regular.ttf#Lobster-Regular</OnPlatform.Android>
            <OnPlatform.WinPhone></OnPlatform.WinPhone>
        </OnPlatform>
    </Label.FontFamily>

new Label {
    Text = "Hello, Forms!",
    FontFamily = Device.OnPlatform (
        null,
        null,
        @"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular"
                 // Windows Phone will use this custom font
    )
}

但是当我运行我的应用程序时,字体没有为 Windows 10 和 8.1 设置。

如何为 Windows 10 和 8.1 设置字体系列。 或者有没有更有效的方法来应用覆盖所有平台的字体系列?

【问题讨论】:

    标签: c# xamarin.forms portable-class-library xamarin.uwp


    【解决方案1】:

    注意:如果您使用的是 NavigationPage,则会出现 custom fonts do not work 的错误

    Windows 8.1/UWP 上的自定义字体不需要自定义渲染器; Xamarin 示例代码只是有几个错误:

    • 改用正斜杠 ('/')
    • 路径格式应为[font file]#[font name](不带字体样式,例如'regular')

    所以这种情况下的路径实际上应该是

    "Assets/Fonts/Lobster-Regular.ttf#Lobster"
    

    你也可以在 Xaml 中使用它

    <OnPlatform.WinPhone>Assets/Fonts/Lobster-Regular.ttf#Lobster</OnPlatform.WinPhone>
    

    确保您的字体文件包含在带有BuildAction:Content 的项目中并且它应该可以工作。

    【讨论】:

      【解决方案2】:

      您可以尝试在 Windows 平台上创建一个 CustomRenderer 覆盖 Xamarin.Forms.Label,如下所示:

      [assembly: ExportRenderer(typeof(Xamarin.Forms.Label), typeof(MyLabelRenderer))]
      namespace MyApp.CustomRenderers.Controls
      {
          public class MyLabelRenderer : LabelRenderer
          {
              protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
              {
                  base.OnElementChanged(e);
      
                  if (Control != null)
                  {
                      var font = new FontFamily(@"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular");
      
                      if (e.NewElement != null)
                      {
                          switch (e.NewElement.FontAttributes)
                          {
                              case FontAttributes.None:
                                  break;
                              case FontAttributes.Bold:
                                  //set bold font etc
                                  break;
                              case FontAttributes.Italic:
                                  //set italic font etc
                                  break;
                              default:
                                  break;
                          }
                      }
                      Control.FontFamily = font;
                  }
              }     
          }
      

      【讨论】:

      • 我应该为所有平台创建自定义渲染器吗?这样我就不必为 android 和 ios 单独设置它了?
      • 从您的示例中,您将iosandroid 上的字体系列设置为null。但是是的,如果您想为该平台上的所有标签设置不同的值,那么我的方法是为每个平台设置一个 CustomRenderer。请记住,如果您想获得所有显示文本的控件,您可能还需要为 EntrySearchBar 做类似的事情
      猜你喜欢
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2018-02-15
      • 2017-12-24
      • 2017-05-19
      • 2019-10-08
      • 1970-01-01
      相关资源
      最近更新 更多