diff --git a/CalligraphySample/src/main/java/uk/co/chrisjenx/calligraphy/sample/PlaceholderFragment.java b/CalligraphySample/src/main/java/uk/co/chrisjenx/calligraphy/sample/PlaceholderFragment.java
index 0404877..0b5cad1 100644
--- a/CalligraphySample/src/main/java/uk/co/chrisjenx/calligraphy/sample/PlaceholderFragment.java
+++ b/CalligraphySample/src/main/java/uk/co/chrisjenx/calligraphy/sample/PlaceholderFragment.java
@@ -1,6 +1,7 @@
package uk.co.chrisjenx.calligraphy.sample;
import android.app.AlertDialog;
+import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@@ -12,6 +13,8 @@
import butterknife.ButterKnife;
import butterknife.OnClick;
+import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
+import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
/**
* A placeholder fragment containing a simple view.
@@ -57,4 +60,19 @@ public void onClick(DialogInterface dialog, int which) {
});
builder.create().show();
}
+
+ @OnClick(R.id.button_non_default_config)
+ public void onClickNonDefaultConfig() {
+ final Context calligraphyContext =
+ CalligraphyContextWrapper.wrap(getActivity(), new CalligraphyConfig.Builder()
+ .setDefaultFontPath("fonts/Oswald-Stencbab.ttf")
+ .setFontAttrId(R.attr.fontPath)
+ .addCustomViewWithSetTypeface(CustomViewWithTypefaceSupport.class)
+ .addCustomStyle(TextField.class, R.attr.textFieldStyle)
+ .build());
+ new android.support.v7.app.AlertDialog.Builder(calligraphyContext)
+ .setTitle(R.string.dialog_non_default_config_title)
+ .setView(R.layout.dialog_non_default_config)
+ .show();
+ }
}
diff --git a/CalligraphySample/src/main/res/layout/dialog_non_default_config.xml b/CalligraphySample/src/main/res/layout/dialog_non_default_config.xml
new file mode 100644
index 0000000..942ad58
--- /dev/null
+++ b/CalligraphySample/src/main/res/layout/dialog_non_default_config.xml
@@ -0,0 +1,109 @@
+
You would rarely need to use this method directly, so unless you need a different + * configuration in multiple contexts in you app, prefer defining a + * {@linkplain CalligraphyConfig#initDefault default configuration} and use + * {@link #wrap(Context)} instead. + * + * @return Context to use as the {@linkplain Activity#attachBaseContext base context} of your + * Activity or for initializing a view hierarchy. + */ + public static ContextWrapper wrap(Context base, CalligraphyConfig config) { + return new CalligraphyContextWrapper(base, config); } /** @@ -72,18 +88,10 @@ static CalligraphyActivityFactory get(Activity activity) { return (CalligraphyActivityFactory) activity.getLayoutInflater(); } - /** - * Uses the default configuration from {@link uk.co.chrisjenx.calligraphy.CalligraphyConfig} - * - * Remember if you are defining default in the - * {@link uk.co.chrisjenx.calligraphy.CalligraphyConfig} make sure this is initialised before - * the activity is created. - * - * @param base ContextBase to Wrap - */ - CalligraphyContextWrapper(Context base) { + CalligraphyContextWrapper(Context base, CalligraphyConfig config) { super(base); - mAttributeId = CalligraphyConfig.get().getAttrId(); + mAttributeId = config.getAttrId(); + mCalligraphyConfig = config; } /** @@ -102,6 +110,7 @@ static CalligraphyActivityFactory get(Activity activity) { public CalligraphyContextWrapper(Context base, int attributeId) { super(base); mAttributeId = attributeId; + mCalligraphyConfig = null; } @Override @@ -112,6 +121,9 @@ public Object getSystemService(String name) { } return mInflater; } + if (CALLIGRAPHY_CONFIG_SERVICE.equals(name)) { + return mCalligraphyConfig != null ? mCalligraphyConfig : CalligraphyConfig.get(); + } return super.getSystemService(name); } 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..3d1331c 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java @@ -36,9 +36,10 @@ protected static int[] getStyleForTextView(TextView view) { styleIds[1] = android.R.attr.subtitleTextStyle; } if (styleIds[0] == -1) { + final CalligraphyConfig config = CalligraphyConfig.from(view.getContext()); // Use TextAppearance as default style - styleIds[0] = CalligraphyConfig.get().getClassStyles().containsKey(view.getClass()) - ? CalligraphyConfig.get().getClassStyles().get(view.getClass()) + styleIds[0] = config.getClassStyles().containsKey(view.getClass()) + ? config.getClassStyles().get(view.getClass()) : android.R.attr.textAppearance; } return styleIds; @@ -117,6 +118,7 @@ public View onViewCreated(View view, Context context, AttributeSet attrs) { } void onViewCreatedInternal(View view, final Context context, AttributeSet attrs) { + final CalligraphyConfig config = CalligraphyConfig.from(context); if (view instanceof TextView) { // Fast path the setting of TextView's font, means if we do some delayed setting of font, // which has already been set by use we skip this TextView (mainly for inflating custom, @@ -142,7 +144,7 @@ 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); - CalligraphyUtils.applyFontToTextView(context, (TextView) view, CalligraphyConfig.get(), textViewFont, deferred); + CalligraphyUtils.applyFontToTextView(context, (TextView) view, config, textViewFont, deferred); } // AppCompat API21+ The ActionBar doesn't inflate default Title/SubTitle, we need to scan the @@ -178,7 +180,7 @@ public void onGlobalLayout() { if (typeface != null) { ((HasTypeface) view).setTypeface(typeface); } - } else if (CalligraphyConfig.get().isCustomViewTypefaceSupport() && CalligraphyConfig.get().isCustomViewHasTypeface(view)) { + } else if (config.isCustomViewTypefaceSupport() && config.isCustomViewHasTypeface(view)) { final Method setTypeface = ReflectionUtils.getMethod(view.getClass(), "setTypeface"); String fontPath = resolveFontPath(context, attrs); Typeface typeface = getDefaultTypeface(context, fontPath); @@ -190,7 +192,7 @@ public void onGlobalLayout() { private Typeface getDefaultTypeface(Context context, String fontPath) { if (TextUtils.isEmpty(fontPath)) { - fontPath = CalligraphyConfig.get().getFontPath(); + fontPath = CalligraphyConfig.from(context).getFontPath(); } if (!TextUtils.isEmpty(fontPath)) { return TypefaceUtils.load(context.getAssets(), fontPath); diff --git a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyLayoutInflater.java b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyLayoutInflater.java index 6f9cfc1..46117c1 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyLayoutInflater.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyLayoutInflater.java @@ -4,6 +4,7 @@ import android.content.Context; import android.os.Build; import android.util.AttributeSet; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -26,6 +27,7 @@ class CalligraphyLayoutInflater extends LayoutInflater implements CalligraphyAct private final int mAttributeId; private final CalligraphyFactory mCalligraphyFactory; + private final CalligraphyConfig mCalligraphyConfig; // Reflection Hax private boolean mSetPrivateFactory = false; private Field mConstructorArgs = null; @@ -34,12 +36,14 @@ protected CalligraphyLayoutInflater(Context context, int attributeId) { super(context); mAttributeId = attributeId; mCalligraphyFactory = new CalligraphyFactory(attributeId); + mCalligraphyConfig = CalligraphyConfig.from(context); setUpLayoutFactories(false); } protected CalligraphyLayoutInflater(LayoutInflater original, Context newContext, int attributeId, final boolean cloned) { super(original, newContext); mAttributeId = attributeId; + mCalligraphyConfig = CalligraphyConfig.from(newContext); mCalligraphyFactory = new CalligraphyFactory(attributeId); setUpLayoutFactories(cloned); } @@ -105,7 +109,7 @@ private void setPrivateFactoryInternal() { // Already tried to set the factory. if (mSetPrivateFactory) return; // Reflection (Or Old Device) skip. - if (!CalligraphyConfig.get().isReflection()) return; + if (!mCalligraphyConfig.isReflection()) return; // Skip if not attached to an activity. if (!(getContext() instanceof Factory2)) { mSetPrivateFactory = true; @@ -195,7 +199,7 @@ private View createCustomViewInternal(View parent, View view, String name, Conte // significant difference to performance on Android 4.0+. // If CustomViewCreation is off skip this. - if (!CalligraphyConfig.get().isCustomViewCreation()) return view; + if (!mCalligraphyConfig.isCustomViewCreation()) return view; if (view == null && name.indexOf('.') > -1) { if (mConstructorArgs == null) mConstructorArgs = ReflectionUtils.getField(LayoutInflater.class, "mConstructorArgs");