nwestfall / Calligraphy.Xamarin

Use custom fonts in Android! Port of https://github.com/InflationX/Calligraphy
Apache License 2.0
3 stars 3 forks source link

Custom fonts not taking affect after switching to Calligraphy.Xamarin #15

Open gonzobrains opened 3 years ago

gonzobrains commented 3 years ago

Hi,

I switched to using your project after I had to upgrade my project to API Level 29. I was previously using Callygraphy.Xamarin, which I believe is deprecated and doesn't support Android 10.

My project builds but I don't see my fonts coming up. I even added the config code to OnCreate() per your instructions. Is this a drop-in replacement or do I have to make more code changes? All of my fonts are under my projects "Asset/fonts" folder.

Thanks, Jeff

nwestfall commented 3 years ago

Can you provide a sample snippet of the code?

What were you using previously?

gonzobrains commented 3 years ago

I added this in MainApplication:OnCreate():

CalligraphyConfig.Builder builder = new CalligraphyConfig.Builder(); CalligraphyConfig config = builder.SetDefaultFontPath("/fonts/Ubuntu-Regular.ttf").Build(); CalligraphyConfig.InitDefault(config);

I didn't have anything here before. My TextViews all have something like this:

fontPath="fonts/Ubuntu-Medium.ttf"

My BaseActivity.cs looks like this:

protected override void AttachBaseContext(Context newBase) { base.AttachBaseContext(CalligraphyContextWrapper.Wrap(newBase)); }

My fonts are in this folder:

image

gonzobrains commented 3 years ago

Just as I suspected, Android can't find my fonts. How do I update the path to work with your library?

image

gonzobrains commented 3 years ago

I just read your TypeFace load() code. I may have to remove the leading "/"

gonzobrains commented 3 years ago

Hi, again. It looks like I resolved the error, but the fonts are still not being used. What else should I try?

nwestfall commented 3 years ago

Are you compiling the fonts correct as assets?

gonzobrains commented 3 years ago

Hi. Thanks for the quick response. I did not originally write the code so I am not familiar with the original usage of the old Calligraphy 2 library.

Is this what you mean by compiling them correctly (ie., setting the Build Action to AndroidAsset)?

image

I've literally left everything in place as it was with the old library (fonts, font paths, layout XML files, etc). The only change I made was to the Application class as I wrote above. Do I need to add an interceptor or something? I didn't see that in your instructions but I see some dialog about it in the underlying native project's github.

Thanks again!

nwestfall commented 3 years ago

What are you referring to with the "Old Calligraphy 2" Library? you mean the original java one?

gonzobrains commented 3 years ago

The mikescandy one:

https://github.com/mikescandy/Calligraphy-xamarin

I uninstalled it, installed yours, and made some tweaks.

nwestfall commented 3 years ago

If you can do a quick test, Android compilers someones don't like having a - in the filename. I know this is mostly for resources, but maybe you can test the fonts as well? Either remove the - or change to a _

gonzobrains commented 3 years ago

I will try this. Is this something that could have changed with API Level 29? The font names haven't changed in a while and I haven't updated VS lately either.

What else should I check while I'm at it?

On Thu, Dec 10, 2020, 2:59 PM Nathan Westfall notifications@github.com wrote:

If you can do a quick test, Android compilers someones don't like having a

  • in the filename. I know this is mostly for resources, but maybe you can test the fonts as well? Either remove the - or change to a _

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nwestfall/Calligraphy.Xamarin/issues/15#issuecomment-742761951, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACY5JXCLEII64MT76PSA4LSUESCXANCNFSM4UVLZORQ .

gonzobrains commented 3 years ago

If you can do a quick test, Android compilers someones don't like having a - in the filename. I know this is mostly for resources, but maybe you can test the fonts as well? Either remove the - or change to a _

I tried this. It didn't help.

gonzobrains commented 3 years ago

Can you explain how to do this (from your documentation)? Perhaps I am missing something:

"Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method and pass it to the CalligraphyInterceptor that you add to your ViewPump builder."

bencat86 commented 3 years ago

I have a similar issue with fonts not loading. I made the same switch from https://github.com/mikescandy/Calligraphy-xamarin as part of an upgrade to API30.

I've found that the default font using SetDefaultFontPath("...") is applied correctly. I'm also using the fontPath property in xml to apply FontAwesome to some controls as needed, but these don't get applied at all. It seems that fontPath is ignored in favour of the default font above.

bencat86 commented 3 years ago

I cloned the source to see what was going on, and I think I've found the issue (at least in my case).

In CalligraphyFactory.OnViewCreatedInternal the font path is correctly resolved from the xml property. The next part then sets the textViewFont variable to null (in my case it's the else branch that gets called):

// Try theme attributes
if(!string.IsNullOrEmpty(textViewFont))
{
    int[] styleForTextView = GetStyleForTextView(textView);
    if (styleForTextView[1] != -1)
        textViewFont = CalligraphyUtils.PullFontPathFromTheme(context, styleForTextView[0], styleForTextView[1], attributeId);
    else
        textViewFont = CalligraphyUtils.PullFontPathFromTheme(context, styleForTextView[0], attributeId);
}

Commenting out the above section loads the font as expected.

Digging deeper, CalligraphyUtils.cs line 238 seems to be setting the null from the typedArray.GetString(0) call.