FontAwesome Pro and xamarin.ios only one font can

2019-02-17 14:44发布

问题:

So I have been looking for an answer to this.

I can't get all 3 fonts in FontAwesome Pro to work with my xamarin native (iOS) to work.

I do use

UIFont.FromName(fontName, size);

But the "fontName" needs to be the "family"-name of the Font, which is a problem with FontAwesome because all versions in the pro edition got the same PostScript name. Its not the filename, its the family property of the font. So when I uses (on Mac) Fontbook, I can see that all three versions (Regular, Light and Solid) of the fontawesome pro, got the same family name, which is "Font Awesome 5 Pro".

So this means that I can only use one of the three fonts in the app.

I was looking for a solution to change the format, but I can't seem to find any way to do so. But if I could set which format I wanted the font to be in, then I wanted to define 'Light', 'Regular' or 'Solid' as font formats...

Heres a screenshot from the Fontbook (Sorry it's in Danish)

回答1:

Those fonts also have Postscript family name defined you can use instead of the primary family name.

I do not have a pro license, but the free v5 show:

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

So you can:

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

FYI: To display those names, use the following:

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);
    }
}


回答2:

To use Font Awesome 5 Pro in Xamarin.Forms

Use it like below... (Thanks to the helpful code of SushiHangover) * Font Awesome 5 Brands *-- 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 RicoShareLabel}">
                <!--<Label.GestureRecognizers>
                    <TapGestureRecognizer
                        Tapped="OnSocialShareClicked" />
                </Label.GestureRecognizers>-->
            </Label>
        </AbsoluteLayout>