diff --git a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyConfig.java b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyConfig.java index 2fb8f63..cb741fa 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyConfig.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyConfig.java @@ -1,5 +1,6 @@ package uk.co.chrisjenx.calligraphy; +import android.graphics.Typeface; import android.os.Build; import android.text.TextUtils; import android.view.View; @@ -91,6 +92,10 @@ public static CalligraphyConfig get() { * The default Font Path if nothing else is setup. */ private final String mFontPath; + /** + * Default typeface to setup if no font path is provided + */ + private final Typeface mTypeface; /** * Default Font Path Attr Id to lookup */ @@ -120,6 +125,7 @@ public static CalligraphyConfig get() { protected CalligraphyConfig(Builder builder) { mIsFontSet = builder.isFontSet; mFontPath = builder.fontAssetPath; + mTypeface = builder.typeface; mAttrId = builder.attrId; mReflection = builder.reflection; mCustomViewCreation = builder.customViewCreation; @@ -137,6 +143,13 @@ public String getFontPath() { return mFontPath; } + /** + * @return typeface provided by the config, might be null + */ + public Typeface getTypeface() { + return mTypeface; + } + /** * @return true if set, false if null|empty */ @@ -200,6 +213,10 @@ public static class Builder { * The default fontPath */ private String fontAssetPath = null; + /** + * The default typeface + */ + private Typeface typeface = null; /** * Additional Class Styles. Can be empty. */ @@ -226,11 +243,23 @@ public Builder setFontAttrId(int fontAssetAttrId) { * @return this builder. */ public Builder setDefaultFontPath(String defaultFontAssetPath) { - this.isFontSet = !TextUtils.isEmpty(defaultFontAssetPath); + this.isFontSet = isFontSet || !TextUtils.isEmpty(defaultFontAssetPath); this.fontAssetPath = defaultFontAssetPath; return this; } + /** + * Set the default type if you don't define in font path or else where in your styles. + * + * @param typeface typeface to set, passing null will fallback to fontpath + * @return + */ + public Builder setDefaultTypeface(Typeface typeface) { + this.isFontSet = isFontSet || typeface != null; + this.typeface = typeface; + return this; + } + /** *

Turn of the use of Reflection to inject the private factory. * This has operational consequences! Please read and understand before disabling. 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 572763b..9f29406 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java @@ -172,7 +172,7 @@ private Typeface getDefaultTypeface(Context context, String fontPath) { if (!TextUtils.isEmpty(fontPath)) { return TypefaceUtils.load(context.getAssets(), fontPath); } - return null; + return CalligraphyConfig.get().getTypeface(); } /** diff --git a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyUtils.java b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyUtils.java index ab71cd9..6030e1c 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyUtils.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyUtils.java @@ -122,7 +122,11 @@ static void applyFontToTextView(final Context context, final TextView textView, static void applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, boolean deferred) { if (context == null || textView == null || config == null) return; if (!config.isFontSet()) return; - applyFontToTextView(context, textView, config.getFontPath(), deferred); + if (!TextUtils.isEmpty(config.getFontPath())) { + applyFontToTextView(context, textView, config.getFontPath(), deferred); + } else { + applyFontToTextView(textView, config.getTypeface(), deferred); + } } /**