Embedded fonts not working on iOS

nnovalbos 371 Reputation points
2021-12-23T12:06:40.61+00:00

Hello,

I am having a problem with embedded fonts

I add 3 fonts to my project: GraphikBold, GraphikRegular and GraphikLight and I add them in the app.xaml.cs

[assembly: ExportFont ("Graphik-Bold-Web.ttf", Alias = "FontGraphikBold")]
[assembly: ExportFont ("Graphik-Light-Web.ttf", Alias = "FontGraphikLight")]
[assembly: ExportFont ("Graphik-Regular-Web.ttf", Alias = "FontGraphikRegular")]

When I use them, if I use GraphinkBold, all the texts already come out with GraphikBold even if I indicate that I want the GraphinkRegular font. This only happens on iOS, on Android it works perfectly.

It's as if it wasn't able to change fonts once loaded ...

I followed this tutorial to do it: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/

Any ideas? thank you.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,451 Reputation points Microsoft Vendor
    2021-12-28T08:08:37.187+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I test the ttf file you provided, the regular and light ones look like the same style. You could try to download GraphikBold.otf, GraphikLight.otf GraphikRegular.otf and use these fonts. I'm afraid this is a issue about Graphik Web font in iOS, I will report it by my internal way. You could also report it in github: https://github.com/xamarin/Xamarin.Forms/issues.

    [assembly: ExportFont("GraphikRegular.otf")]  
    [assembly: ExportFont("GraphikLight.otf")]  
    [assembly: ExportFont("GraphikBold.otf")]  
    

    Xaml

     <Label x:Name="boldLabel" Text="Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!" FontFamily="GraphikBold"  HorizontalTextAlignment="Center" />  
                <Label x:Name="lightLabel" Text="Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!" FontFamily="GraphikLight"   HorizontalTextAlignment="Center" />  
                <Label x:Name="regularLabel" Text="Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!Welcome to Xamarin.Forms!" FontFamily="GraphikRegular"  HorizontalTextAlignment="Center" />  
    

    In addition, this is my test, you could also test other fonts, refer to :

    I download some other fonts( GraphikBold.otf, GraphikLight.otf ) and add them to my project, it works.
    So I install the font you provided into my Mac, I got an error when I install the font. Then I add the otf ttf files to Resources folder in iOS project , add the font to info.plist and print all font name.
    ** info.plist **

    <key>UIAppFonts</key>  
    	<array>  
    		<string>Graphik-Bold-Web.ttf</string>  
    		<string>Graphik-Light-Web.ttf</string>  
    		<string>Graphik-Regular-Web.ttf</string>  
      
    		<string>GraphikRegular.otf</string>  
    		<string>GraphikLight.otf</string>  
    		<string>GraphikBold.otf</string>  
    	</array>  
    

    Print font name

      #if DEBUG  
                    var fontList = new StringBuilder();  
                    var familyNames = UIFont.FamilyNames;  
                    foreach (var familyName in familyNames)  
                    {  
                        fontList.Append(String.Format("Family: {0}\n", familyName));  
                        Console.WriteLine("Family: {0}\n", familyName);  
                        var fontNames = UIFont.FontNamesForFamilyName(familyName);  
                        foreach (var fontName in fontNames)  
                        {  
                            Console.WriteLine("\tFont: {0}\n", fontName);  
                            fontList.Append(String.Format("\tFont: {0}\n", fontName));  
                        }  
                    };  
        #endif  
    

    When I build and run the project, the font name whose font family name is Graphik Web is deferent every time . In addition, I add three files but it only print one, I'm afraid the other style are not supported for iOS.
    only the Bold one

    [assembly: ExportRenderer(typeof(Label), typeof(MyLabelRender))]  
    namespace EmbeddedFontsSample.iOS  
    {  
       public class MyLabelRender : LabelRenderer  
        {  
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)  
            {  
                base.OnElementChanged(e);  
                if (Control != null)  
                {  
                    var fontNames = UIFont.FontNamesForFamilyName("Graphik Web");  
      
                    Control.Font = UIFont.FromName(fontNames[0], 16);  
      
                }  
            }  
        }  
    }  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful