为什么我的自定义字体在 Xamarin.iOS 中不起作用? [英] Why are my custom fonts not working in Xamarin.iOS?

查看:32
本文介绍了为什么我的自定义字体在 Xamarin.iOS 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

男孩,我被困住了.我想我已经在线查看了每个指南,但无法使其正常工作.以下是我已完成的步骤:

  1. 我已将此自定义字体目录添加到我的 Xamarin.iOS 项目中在资源/字体下.
  2. 我已将他们的构建操作全部设置为
    捆绑资源.
  3. 我已为他们设置始终复制到构建中
    目录.
  4. 我已将他们的名字添加到 Info.Plist 中,如下所示:

UIAppFonts<阵列><string>BentonSans-Regular.otf</string><string>BentonSans-Black.otf</string><string>BentonSans-Bold.otf</string><string>BentonSans-Book.otf</string><string>BentonSans-CompMediumRegular.otf</string><string>BentonSans-Medium.otf</string></阵列>

  1. 我尝试像这样设置字体:

    button.Font = UIFont.FromName("BentonSansComp-Medium", 16);

请注意这样做,我的应用程序运行,但字体似乎没有任何效果.我曾尝试将 Fonts/ 放在 info.plist 中的字体前面,但这会导致代码中引用字体为空的异常.

我真的无法弄清楚我在这里遗漏了什么.字体似乎不适用.我使用 SushiHangover 的

这是我的 info.plist.(构建操作:BundleResource,复制到输出目录:始终复制)

UIAppFonts<阵列><string>Fonts/MyFonts/Winter.otf</string><string>Fonts/MyFonts/Goodest.ttf</string></阵列>

测试代码:

var lab1 = new UILabel(new RectangleF(0, 100, 300, 40));lab1.Text = "这是一些示例文本";lab1.Font = UIFont.FromName("Winter", 20f);this.Add(lab1);

Boy I'm stuck. I think I've looked at every guide online for this and cannot get it to work. Here are the steps I've done:

  1. I've added this directory of custom fonts to my Xamarin.iOS project under Resources/Fonts.
  2. I've set their buildactions all as
    BundledResources.
  3. I've set for them to always copy to the build
    directory.
  4. I've added their names to Info.Plist like so:

<key>UIAppFonts</key>
    <array>
      <string>BentonSans-Regular.otf</string>
      <string>BentonSans-Black.otf</string>
      <string>BentonSans-Bold.otf</string>
      <string>BentonSans-Book.otf</string>
      <string>BentonSans-CompMediumRegular.otf</string>
      <string>BentonSans-Medium.otf</string>
    </array>

  1. I try setting the font like so:

    button.Font = UIFont.FromName("BentonSansComp-Medium", 16);

Please note doing it this way, my app runs, but the fonts don't seem to take any effect. I have tried putting Fonts/ in front of the fonts in info.plist, but this causes an exception where the referenced font is null in the code.

I really can't work out what I'm missing here. The fonts just don't seem to apply. I used SushiHangover's code to ensure that my fonts are registered:

2021-05-11 16:03:01.168473+0100 MyProject[54240:1851117]  * BentonSans Black
2021-05-11 16:03:01.169049+0100 MyProject[54240:1851117]  *-- BentonSans-Black
2021-05-11 16:03:01.169423+0100 MyProject[54240:1851117]  * BentonSans Bold
2021-05-11 16:03:01.169862+0100 MyProject[54240:1851117]  *-- BentonSans-Bold
2021-05-11 16:03:01.170201+0100 MyProject[54240:1851117]  * BentonSans Book
2021-05-11 16:03:01.170743+0100 MyProject[54240:1851117]  *-- BentonSans-Book
2021-05-11 16:03:01.171092+0100 MyProject[54240:1851117]  * BentonSans Comp Medium
2021-05-11 16:03:01.171826+0100 MyProject[54240:1851117]  *-- BentonSansComp-Medium
2021-05-11 16:03:01.172315+0100 MyProject[54240:1851117]  * BentonSans Medium
2021-05-11 16:03:01.173718+0100 MyProject[54240:1851117]  *-- BentonSans-Medium
2021-05-11 16:03:01.174237+0100 MyProject[54240:1851117]  * BentonSans Regular
2021-05-11 16:03:01.175211+0100 MyProject[54240:1851117]  *-- BentonSans-Regular

This showed me that I was getting the name wrong for BentonSansComp-Medium which did fix a runtime error earlier. But it didn't make the font apply.

解决方案

You used string "BentonSansComp-Medium" as the parameter of method "UIFont.FromName", but it doesn't exist in your plist.

I just followed the blog Using custom Fonts in Xamarin.iOS, and test with .ttf and .otf. Everything works fine.

Folder in Solution Explorer:

Here is my info.plist. (Build Action: BundleResource, Copy to Output Directory: Copy always)

<key>UIAppFonts</key>
<array>
  <string>Fonts/MyFonts/Winter.otf</string>
  <string>Fonts/MyFonts/Goodest.ttf</string>
</array>

test code:

var lab1 = new UILabel(new RectangleF(0, 100, 300, 40));
lab1.Text = "This is some sample text";
lab1.Font = UIFont.FromName("Winter", 20f);
this.Add(lab1);

这篇关于为什么我的自定义字体在 Xamarin.iOS 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆