【问题标题】:FontAwesome Pro and xamarin.ios only one font can be activeFontAwesome Pro 和 xamarin.ios 只能激活一种字体
【发布时间】:2018-01-10 13:06:58
【问题描述】:

所以我一直在寻找这个问题的答案。

我无法让 FontAwesome Pro 中的所有 3 种字体与我的 xamarin 本机 (iOS) 一起工作。

我确实用过

UIFont.FromName(fontName, size);

但是“fontName”需要是字体的“family”名称,这是 FontAwesome 的一个问题,因为专业版中的所有版本都有相同的 PostScript 名称。它不是文件名,而是字体的家族属性。因此,当我使用(在 Mac 上)Fontbook 时,我可以看到 fontawesome pro 的所有三个版本(Regular、Light 和 Solid)都有相同的家族名称,即“Font Awesome 5 Pro”。

所以这意味着我只能在应用程序中使用三种字体中的一种。

我一直在寻找更改格式的解决方案,但似乎找不到任何方法。但如果我可以设置我希望字体采用的格式,那么我想将“Light”、“Regular”或“Solid”定义为字体格式...

这是字体册的截图(抱歉是丹麦语)

【问题讨论】:

  • 你可以像这样使用它们:Regular: UIFont.FromName("FontAwesome5ProRegular", size);实心:UIFont.FromName("FontAwesome5ProSolid", size);

标签: ios xamarin fonts xamarin.ios


【解决方案1】:

这些字体还定义了 Postscript 系列名称,您可以使用它来代替主要系列名称。

我没有专业版许可证,但有免费的 v5 节目:

  * Font Awesome 5 Free
  *-- FontAwesome5FreeRegular
  *-- FontAwesome5FreeSolid

所以你可以:

var font = UIFont.FromName(@"FontAwesome5FreeSolid", 20);
var font = UIFont.FromName(@"FontAwesome5FreeRegular", 20);

仅供参考:要显示这些名称,请使用以下内容:

foreach (var familyNames in UIFont.FamilyNames.OrderBy(c => c).ToList())
{
    Console.WriteLine(" * " + familyNames);
    foreach (var familyName in UIFont.FontNamesForFamilyName(familyNames).OrderBy(c => c).ToList())
    {
        Console.WriteLine(" *-- " + familyName);
    }
}

【讨论】:

  • 谢谢先生!我确信我已经使用 PostScript 名称进行了测试,但我一定是做错了什么。它现在可以工作了,我有一个 FontHelper 类来捕获需要使用的字体系列。
  • 谢谢,也可以使用 Postscript 姓氏与 xamarin 一起使用
  • 我发现现在免费的Brands集合名为"FontAwesome5BrandsRegular"
  • 把这个留在这里,以防它帮助任何人。此答案中的字体名称对我不起作用,但是我使用您的代码显示名称并发现“FontAwesome5Free-Solid”和“FontAwesome5Free-Regular”最终确实有效。 2019 年 11 月从 fontawesome 下载了这些文件。
  • 我的字体的名称与我预期的完全不同!你的代码帮我弄清楚了
【解决方案2】:

在 Xamarin.Forms 中使用 Font Awesome 5 Pro

像下面这样使用它... (感谢SushiHangover的有用代码)

  • 字体真棒 5 个品牌 *-- FontAwesome5BrandsRegular
  • Font Awesome 5 Pro *-- FontAwesome5ProLight *-- FontAwesome5ProRegular *-- FontAwesome5ProSolid

App.Xaml.cs

        Current.Resources = new ResourceDictionary();
        // Font awesome
        Current.Resources["FontawesomeSolid"] = Device.RuntimePlatform == Device.iOS ? "Font Awesome 5 Pro" : "fa-solid-900.ttf#Font Awesome 5 Pro";
        Current.Resources["FontawesomeRegular"] = Device.RuntimePlatform == Device.iOS ? "FontAwesome5Regular" : "fa-regular-400.ttf#Font Awesome 5 Pro";
        Current.Resources["FontawesomeLight"] = Device.RuntimePlatform == Device.iOS ? "FontAwesome5ProLight" : "fa-light-300.ttf#Font Awesome 5 Pro";
        Current.Resources["FontawesomeBrands"] = Device.RuntimePlatform == Device.iOS ? "FontAwesome5ProBrands" : "fa-brands-400.ttf#fFont Awesome 5 Pro";


        Current.Resources.Add("ShareIconLabel", new Style(typeof(Label))
        {
            Setters =
            {
                new Setter { Property = Label.TextColorProperty, Value = Color.White},
                new Setter { Property = View.HorizontalOptionsProperty, Value = LayoutOptions.End},
                new Setter { Property = View.VerticalOptionsProperty, Value = LayoutOptions.Center},
                new Setter { Property = AbsoluteLayout.LayoutBoundsProperty, Value = new Rectangle (0.90, 0.5, 0.2, 1)},
                new Setter { Property = AbsoluteLayout.LayoutFlagsProperty, Value = AbsoluteLayoutFlags.All},
                new Setter { Property = Label.FontSizeProperty, Value = 30},
                new Setter { Property = Label.FontFamilyProperty, Value = Current.Resources["FontawesomeLight"] },
                new Setter { Property = Label.TextProperty, Value = "\uf1e0" } // Share Icon
            }
        });

MySharePage.Xaml

        <AbsoluteLayout
            AbsoluteLayout.LayoutBounds="0,1,1,0.07"
            AbsoluteLayout.LayoutFlags="All"
            BackgroundColor="{x:Static localColors:Constants.MyOrange} ">
            <Label
                Style="{StaticResource ShareIconLabel}">
                <!--<Label.GestureRecognizers>
                    <TapGestureRecognizer
                        Tapped="OnSocialShareClicked" />
                </Label.GestureRecognizers>-->
            </Label>
        </AbsoluteLayout>

【讨论】:

    【解决方案3】:

    iOS 仅通过其“PostScript 名称”加载字体。您可以重命名您的 TTF 文件以匹配该 PostScript 名称,这样会更容易正确使用。

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 2014-05-22
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多