Custom font not working in lollipop

2020-02-11 07:21发布

问题:

Is there any way to use geomanist(custom) font in android lollipop.

But it works in all other version.

Here is my code

In MyApplication class

    FontsOverride.setDefaultFont(this, "DEFAULT", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "MONOSPACE", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "SERIF", "geomanist-lightnew.ttf");
    FontsOverride.setDefaultFont(this, "SANS_SERIF", "geomanist-lightnew.ttf");

public final class FontsOverride {

public static void setDefaultFont(Context context,
                                  String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typeface.createFromAsset(context.getAssets(),
            fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

protected static void replaceFont(String staticTypefaceFieldName,
                                  final Typeface newTypeface) {
    try {
        final Field staticField = Typeface.class
                .getDeclaredField(staticTypefaceFieldName);
        staticField.setAccessible(true);
        staticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }



}




}

Thanks

回答1:

I use this Library and it works superbly on all devices

Step:-

1) Add this line in your gradle compile 'com.github.balrampandey19:FontOnText:0.0.1'

2) Add your font in asset folder like this

3) Then replace your view in xml with

<com.balram.library.FotTextView
                    android:id="@+id/vno_tv"
                    .
                    .
                    android:textSize="14sp"
                    app:font="regular.ttf" />

this line is important to set custom font you want app:font="regular.ttf"

You can do same for Buttons Edittext

OR

If you want to use same "Font" through whole application you can follow this Guide here



回答2:

  1. The root cause of the problem is badly written font files, so it makes sense that the solution is to re-encode the offending font. You can use a online converting tool such as http://www.freefontconverter.com/.

  2. Another Alternative is using a library for custom fonts like https://github.com/chrisjenx/Calligraphy

dependencies {
            compile 'uk.co.chrisjenx:calligraphy:2.2.0'
        }