From 8decc7ad2db8d17d628a79ad272582e28e1cfd60 Mon Sep 17 00:00:00 2001 From: Nizam Moidu Date: Wed, 29 Jul 2015 13:31:13 +0400 Subject: [PATCH 1/5] use beta4 build tools --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index d58e793..050373b 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.3.0-beta3' + classpath 'com.android.tools.build:gradle:1.3.0-beta4' } } From 00db618eb0434af613a4971e8f9411fcf3738bd0 Mon Sep 17 00:00:00 2001 From: Nizam Moidu Date: Wed, 29 Jul 2015 13:36:33 +0400 Subject: [PATCH 2/5] support bold, italic , bolditalic postfixes for missing fonts If the fontPath is invalid in assets we postfix them corresponding to original type face style and try to set it --- .../calligraphy/CalligraphyFactory.java | 19 +++++++++++++++++ .../chrisjenx/calligraphy/TypefaceUtils.java | 21 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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 ca45b62..913e1d8 100644 --- a/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java +++ b/calligraphy/src/main/java/uk/co/chrisjenx/calligraphy/CalligraphyFactory.java @@ -3,6 +3,7 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; +import android.graphics.Typeface; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; @@ -149,6 +150,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; + } + } } From e7dd8f72a078b2016fabace56614774a80ff4dc4 Mon Sep 17 00:00:00 2001 From: Nizam Moidu Date: Wed, 29 Jul 2015 13:44:02 +0400 Subject: [PATCH 3/5] update sample --- .../src/main/res/layout/fragment_main.xml | 11 +++++++++-- CalligraphySample/src/main/res/values/strings.xml | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CalligraphySample/src/main/res/layout/fragment_main.xml b/CalligraphySample/src/main/res/layout/fragment_main.xml index 4c7bc46..622c99a 100644 --- a/CalligraphySample/src/main/res/layout/fragment_main.xml +++ b/CalligraphySample/src/main/res/layout/fragment_main.xml @@ -1,5 +1,5 @@ - + + \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 From 3ea6fef8177911d463e9f4d2194bf5a5f5bff358 Mon Sep 17 00:00:00 2001 From: Nizam Moidu Date: Wed, 29 Jul 2015 14:35:20 +0400 Subject: [PATCH 4/5] update read me for textStyle attribute support --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8295323..5cb5666 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 From 963e21147755e122467a19909fffae15dc2a20b2 Mon Sep 17 00:00:00 2001 From: Nizam Moidu Date: Wed, 29 Jul 2015 14:39:15 +0400 Subject: [PATCH 5/5] remove app prefix in the sample to align with documentation --- CalligraphySample/src/main/res/layout/fragment_main.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CalligraphySample/src/main/res/layout/fragment_main.xml b/CalligraphySample/src/main/res/layout/fragment_main.xml index 622c99a..e393db8 100644 --- a/CalligraphySample/src/main/res/layout/fragment_main.xml +++ b/CalligraphySample/src/main/res/layout/fragment_main.xml @@ -1,5 +1,5 @@ - + fontPath="fonts/Roboto.ttf" />