Parcourir la source

Added global typeface theming support. TTF fonts will also no longer be enforced for typeface(String, String). Resolves https://github.com/afollestad/material-dialogs/issues/474.

Aidan Follestad il y a 10 ans
Parent
commit
cffd86ee07

BIN
library/src/main/assets/fonts/Roboto-Medium.ttf


BIN
library/src/main/assets/fonts/Roboto-Regular.ttf


+ 0 - 10
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -5,7 +5,6 @@ import android.content.res.ColorStateList;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
-import android.text.InputFilter;
 import android.text.InputType;
 import android.text.method.LinkMovementMethod;
 import android.text.method.PasswordTransformationMethod;
@@ -23,7 +22,6 @@ import com.afollestad.materialdialogs.internal.MDButton;
 import com.afollestad.materialdialogs.internal.MDTintHelper;
 import com.afollestad.materialdialogs.simplelist.MaterialSimpleListAdapter;
 import com.afollestad.materialdialogs.util.DialogUtils;
-import com.afollestad.materialdialogs.util.TypefaceHelper;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -61,14 +59,6 @@ class DialogInit {
     public static void init(final MaterialDialog dialog) {
         final MaterialDialog.Builder builder = dialog.mBuilder;
 
-        // Check if default library fonts should be used
-        if (!builder.useCustomFonts) {
-            if (builder.mediumFont == null)
-                builder.mediumFont = TypefaceHelper.get(dialog.getContext(), "Roboto-Medium");
-            if (builder.regularFont == null)
-                builder.regularFont = TypefaceHelper.get(dialog.getContext(), "Roboto-Regular");
-        }
-
         // Set cancelable flag and dialog background color
         dialog.setCancelable(builder.cancelable);
         if (builder.backgroundColor == 0)

+ 19 - 14
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -366,7 +366,6 @@ public class MaterialDialog extends DialogBase implements
         protected boolean autoDismiss = true;
         protected Typeface regularFont;
         protected Typeface mediumFont;
-        protected boolean useCustomFonts;
         protected Drawable icon;
         protected boolean limitIconToDefaultSize;
         protected int maxIconSize = -1;
@@ -451,6 +450,25 @@ public class MaterialDialog extends DialogBase implements
             this.btnStackedGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_btnstacked_gravity, this.btnStackedGravity);
             this.itemsGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_items_gravity, this.itemsGravity);
             this.buttonsGravity = DialogUtils.resolveGravityEnum(context, R.attr.md_buttons_gravity, this.buttonsGravity);
+
+            final String mediumFont = DialogUtils.resolveString(context, R.attr.md_medium_font);
+            final String regularFont = DialogUtils.resolveString(context, R.attr.md_regular_font);
+            typeface(mediumFont, regularFont);
+
+            if (this.mediumFont == null) {
+                try {
+                    this.mediumFont = Typeface.create("sans-serif-medium", Typeface.NORMAL);
+                } catch (Throwable ignored) {
+                }
+            }
+            if (this.regularFont == null) {
+                try {
+                    this.regularFont = Typeface.create("sans-serif", Typeface.NORMAL);
+                } catch (Throwable ignored) {
+                }
+            }
+            if (this.mediumFont == null)
+                this.mediumFont = this.regularFont;
         }
 
         private void checkSingleton() {
@@ -526,17 +544,6 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
-        /**
-         * Disable usage of the default fonts. This is automatically set by
-         * {@link #typeface(String, String)} and {@link #typeface(Typeface, Typeface)}.
-         *
-         * @return The Builder instance so you can chain calls to it.
-         */
-        public Builder disableDefaultFonts() {
-            this.useCustomFonts = true;
-            return this;
-        }
-
         /**
          * Sets the fonts used in the dialog. It's recommended that you use {@link #typeface(String, String)} instead,
          * to avoid duplicate Typeface allocations and high memory usage.
@@ -548,7 +555,6 @@ public class MaterialDialog extends DialogBase implements
         public Builder typeface(Typeface medium, Typeface regular) {
             this.mediumFont = medium;
             this.regularFont = regular;
-            this.useCustomFonts = true;
             return this;
         }
 
@@ -571,7 +577,6 @@ public class MaterialDialog extends DialogBase implements
                 if (this.regularFont == null)
                     throw new RuntimeException("No font asset found for " + regular);
             }
-            this.useCustomFonts = this.mediumFont != null || this.regularFont != null;
             return this;
         }
 

+ 7 - 0
library/src/main/java/com/afollestad/materialdialogs/util/DialogUtils.java

@@ -7,6 +7,7 @@ import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
 import android.support.annotation.AttrRes;
+import android.util.TypedValue;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 
@@ -39,6 +40,12 @@ public class DialogUtils {
         }
     }
 
+    public static String resolveString(Context context, @AttrRes int attr) {
+        TypedValue v = new TypedValue();
+        context.getTheme().resolveAttribute(attr, v, true);
+        return (String) v.string;
+    }
+
     private static int gravityEnumToAttrInt(GravityEnum value) {
         switch (value) {
             case CENTER:

+ 1 - 1
library/src/main/java/com/afollestad/materialdialogs/util/TypefaceHelper.java

@@ -33,7 +33,7 @@ public class TypefaceHelper {
             if (!cache.containsKey(name)) {
                 try {
                     Typeface t = Typeface.createFromAsset(
-                            c.getAssets(), String.format("fonts/%s.ttf", name));
+                            c.getAssets(), String.format("fonts/%s", name));
                     cache.put(name, t);
                     return t;
                 } catch (RuntimeException e) {

+ 3 - 0
library/src/main/res/values/attrs.xml

@@ -63,4 +63,7 @@
         <attr name="md_reduce_padding_no_title_no_buttons" format="boolean" />
     </declare-styleable>
 
+    <attr name="md_medium_font" format="string" />
+    <attr name="md_regular_font" format="string" />
+
 </resources>

BIN
sample/src/main/assets/fonts/GoodDog.otf