xamarin - 无法让 FontAwesome 5 与 Xamarin Forms、iOS 和 Android 配合使用

标签 xamarin xamarin.forms font-awesome

我尝试遵循我能找到的所有教程,并且最关注的是这个:

https://github.com/coolc0ders/FontAwesome5XamarinForms

但是出现的只是问号而不是图标,甚至上面的示例在我运行时也不起作用。

我的 XML 如下所示:

<Label Text="&#xf249;"  FontSize="20" Grid.Column="0" >
   <Label.FontFamily>
      <OnPlatform 
         x:TypeArguments="x:String"
            Android="Font Awesome 5 Free-Regular-400.otf#Font Awesome 5 Free Regular" 
            iOS="Font Awesome 5 Free" />
   </Label.FontFamily>
</Label>

我现在有一些问题。

  • 我需要将 FontFamily 设置为“FontAwesome”
  • 对于 Android 和 iOS,我应该使用 .ttf 文件还是 .otf 文件
  • 对于 iOS,我是否需要编辑 info.plist?如果需要,我该怎么做?
  • 我应该指定我的图标,如 """\uf004"
  • 对于 Android 和 iOS d0,我需要将字体文件设置为“复制到输出目录”
  • 如何找到字体的名称?我将其设置为“Font Awesome 5 Free”,但这只是复制示例,我也无法让它工作。
  • 我应该为 iOS=""指定什么,应该是文件名减去 .ttf (或 .otf)吗? Android= 怎么样?

很抱歉,如果这些问题看起来很基本,但我只是在很长一段时间后无法显示图标。出现的只是一个?除了不起作用的示例之外,我似乎找不到任何地方可以解释如何为 FontAwesome 5 和 Xamarin 执行此操作。

最佳答案

FontAwesome 5 比 FontAwesome 4 稍微复杂一点,因为你有 3 组字体。我已经为 font Awesome 5 pro 做了这个。

Do I need to set FontFamily to "FontAwesome"

对于 iOS,如果您使用品牌集或 Font,则可以将 fontfamily 设置为 Font Awesome 5 Brands Awesome 5 Pro,如果您使用其他 3 套中的任何一套。目前,您一次只能使用一组。我会给您另一个答案,以便您能够在同一应用程序中使用这 3 套。

对于Android,这是我的开关:

switch (awesomeStyle)
            {
                case AwesomeStyle.Brands:
                    fontFile = "fa-brands-400.ttf#Font Awesome 5 Brands";
                    break;
                case AwesomeStyle.Light:
                    fontFile = "fa-light-300.ttf#Font Awesome 5 Pro";
                    break;
                case AwesomeStyle.Regular:
                    fontFile = "fa-regular-400.ttf#Font Awesome 5 Pro";
                    break;
                case AwesomeStyle.Solid:
                    fontFile = "fa-solid-900.ttf#Font Awesome 5 Pro";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

For Android & iOS should I use a .ttf file or a .otf file

我使用 .ttf 文件。

For iOS do I need to edit the info.plist and if so how do I do that?

是的。将以下 key 添加到您的 info.plist 文件中。在浅色、常规或实色之间仅添加一根字符串。

<key>UIAppFonts</key>
<array>
    <string>fa-brands-400.ttf</string>
    <string>fa-light-300.ttf</string>
    <string>fa-regular-400.ttf</string>
    <string>fa-solid-900.ttf</string>
</array>

Should I specify my icon like "" or like "\uf004"

在 xaml 中,您使用 "" 版本。在 C# 中,您使用 "\uf004" 版本。

For Android & iOS d0 I need to set the font files as "copy to output directory"

How do I find out the name of the font? I set it to "Font Awesome 5 Free" but that was just copying the example and I can't get that to work either.

我给了你上面的名字。否则,您应该使用字体编辑器来查找名称。在 Windows 中,您可以在字体文件的属性中找到名称,但信息太多,您应该忽略标题的最后一个单词(在详细信息选项卡中)。

What should I specify for iOS=" ", should that be the filename less the .ttf (or .otf)? How about Android= ?

见上文。

关于xamarin - 无法让 FontAwesome 5 与 Xamarin Forms、iOS 和 Android 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919842/

相关文章:

c# - Xamarin 表单工具栏项未显示在 View 中

android - 如何将 Image.Source 设置为内部存储图片文件夹中的照片?

xamarin.forms - 适用于 UWP 的 Xamarin 表单 Collection View ItemsUpdatingScrollMode

css - Internet Explorer 11+ 不呈现文本溢出 : Elipses Properly With Font Awesome

ios - Xcode Storyboard - 使用很棒的字体制作文本 RTL

c# - 使用 Xamarin 进行 Sha512 哈希处理

c# - 在 iOS 上录制实时音频流

c# - 我可以将 xaml ApplicationResources 与 MvvmCross 应用程序一起使用吗

xamarin - 获取xamarin表单中的当前屏幕宽度

html - 无法左对齐带有很棒图标的文本输入