0
votes
  1. Designer crashed when using the ttf files from the Assets folder.
  2. In my code, I used the ttf files to set the TextView Typeface property(like Typeface = Typeface.CreateFromAsset(this.Context.Assets, "fonts/Sample_Icons.ttf"),
  3. It crashes my designer page.

Please suggest to me.

2

2 Answers

1
votes

When you put the ttf files in the Assets folder, you can access the ttf file in the following method:

 AssetManager assets = this.Assets;
 Typeface font = Typeface.CreateFromAsset(assets, "Lobster-Regular.ttf");

 // and use like this
 Button button = (Button)FindViewById(Resource.Id.btn);
 button.SetTypeface(font, TypefaceStyle.Normal);

In other words, you just need remove the fonts before the ttf file, you can use like this:

Typeface.CreateFromAsset(this.Assets, "Sample_Icons.ttf");

instead of :

Typeface.CreateFromAsset(this.Context.Assets, "fonts/Sample_Icons.ttf");

There is a simple demo, you can check it here .The effect is as follows: enter image description here

0
votes

I used the solution based on this post:

https://blog.mzikmund.com/2017/07/checking-for-design-mode-in-xamarin-forms/

public static class EmulatorHelper
{
    // https://blog.mzikmund.com/2017/07/checking-for-design-mode-in-xamarin-forms/
    public static bool IsDesigner { get; set; }
    #if !RELEASE
        = true;
    #endif
}

Then somewhere in the app start, like for example AndroidApp.cs

public class AndroidApp : App
{
    public override void Initialize()
    {
        base.Initialize();
        EmulatorHelper.IsDesigner = false;
    }
}

And finally replace

Typeface font = Typeface.CreateFromAsset(assets, "Lobster-Regular.ttf");

with

Typeface font = !EmulatorHelper.IsDesigner 
    ? Typeface.CreateFromAsset(assets, "Lobster-Regular.ttf")
    : Typeface.Default;

Drawback of this solution is that you will see default font in the designer, but it is much better than orange box with error label on it :)