diff --git a/CalligraphySample/src/main/res/layout/fragment_main.xml b/CalligraphySample/src/main/res/layout/fragment_main.xml index 7dff524..5f7b5c0 100644 --- a/CalligraphySample/src/main/res/layout/fragment_main.xml +++ b/CalligraphySample/src/main/res/layout/fragment_main.xml @@ -71,6 +71,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/defined_custom_view"/> + \nThis is a custom TextView with Oswald font.\n \nThis is a TextView inflated from a ViewStub.\n \nThis is a TextView inflated from a ViewStub w/ fontPath declared.\n + \nThis has a font path set to Roboto, on the View and it is Bold because of the textStyle\n Custom Oswald checkbox diff --git a/README.md b/README.md index 7fd47a5..2a395c8 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,15 @@ http://schemas.android.com/tools"`. See https://code.google.com/p/android/issues ``` - +### Custom font with textStyle ( bold | italic | normal ) +copy fonts to assets folder by following the name pattern (example for font Roboto) +``` + Normal => fonts/-Regular. (eg:- fonts/Roboto-Regular.ttf) + Bold => fonts/-Bold. (eg:- fonts/Roboto-Bold.ttf) + Italic => fonts/-Italic. (eg:- fonts/Roboto-Italic.ttf) + BoldItalic => fonts/-BoldItalic. (eg:- fonts/Roboto-BoldItalic.ttf) +``` +use `fonts/.` ( eg:- `fonts/Roboto.ttf`) as value for your style/view/theme fontPath attribute.And make sure that there is no actual file with name `fonts/.` ( eg:- `fonts/Roboto.ttf`) in the assets folder #FAQ ### Font Resolution diff --git a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java index dbff457..5502529 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java @@ -142,6 +142,24 @@ void onViewCreatedInternal(View view, final Context context, AttributeSet attrs) // Still need to defer the Native action bar, appcompat-v7:21+ uses the Toolbar underneath. But won't match these anyway. final boolean deferred = matchesResourceIdName(view, ACTION_BAR_TITLE) || matchesResourceIdName(view, ACTION_BAR_SUBTITLE); + if (textViewFont != null && + !"".equalsIgnoreCase(textViewFont.trim()) && + ! TypefaceUtils.checkFontExists(context, textViewFont)) { + String fontPostFix = "Regular"; + TextView textView = (TextView) view; + Typeface typeface = textView.getTypeface(); + if (typeface != null) { + if (typeface.isBold()) { + fontPostFix = "Bold"; + } + if (typeface.isItalic()) { + fontPostFix += "Italic"; + } + int ix = textViewFont.lastIndexOf("."); + textViewFont = textViewFont.substring(0, ix) + "-" + fontPostFix + textViewFont.substring(ix); + } + } + CalligraphyUtils.applyFontToTextView(context, (TextView) view, CalligraphyConfig.get(), textViewFont, deferred); } diff --git a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/TypefaceUtils.java b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/TypefaceUtils.java index 8ad3a8c..0cdecbc 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/TypefaceUtils.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/TypefaceUtils.java @@ -1,10 +1,14 @@ package uk.co.chrisjenx.calligraphy; +import android.content.Context; import android.content.res.AssetManager; import android.graphics.Typeface; import android.util.Log; +import java.io.InputStream; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -20,6 +24,8 @@ public final class TypefaceUtils { private static final Map sCachedFonts = new HashMap(); private static final Map sCachedSpans = new HashMap(); + private static final List sMissingFontPaths = new ArrayList<>(); + private static final List sValidFontPaths = new ArrayList<>(); /** * A helper loading a custom font. @@ -75,4 +81,19 @@ public static boolean isLoaded(Typeface typeface) { private TypefaceUtils() { } + + public static boolean checkFontExists(Context context, String fontPath) { + if (sMissingFontPaths.contains(fontPath)) return false; + if (sValidFontPaths.contains(fontPath)) return true; + AssetManager mg = context.getAssets(); + try { + InputStream is = mg.open(fontPath); + is.close(); + sValidFontPaths.add(fontPath); + return true; + } catch (Exception ex) { + sMissingFontPaths.add(fontPath); + return false; + } + } }