Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Regular/Bold/Italic based on textStyle attribute #195

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CalligraphySample/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/defined_custom_view"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/defined_font_path_text_style"
android:textStyle="bold"
fontPath="fonts/Roboto.ttf" />

<uk.co.chrisjenx.calligraphy.sample.CustomViewWithTypefaceSupport
fontPath="fonts/Oswald-Stencbab.ttf"
Expand Down
1 change: 1 addition & 0 deletions CalligraphySample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="defined_custom_view">\nThis is a custom TextView with Oswald font.\n</string>
<string name="defined_view_stub">\nThis is a TextView inflated from a ViewStub.\n</string>
<string name="defined_view_stub_font_path">\nThis is a TextView inflated from a ViewStub w/ fontPath declared.\n</string>
<string name="defined_font_path_text_style">\nThis has a font path set to Roboto, on the View and it is Bold because of the textStyle\n</string>

<string name="checkbox_custom">Custom Oswald checkbox</string>

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ http://schemas.android.com/tools"`. See https://code.google.com/p/android/issues
</style>
```


### Custom font with textStyle ( bold | italic | normal )
copy fonts to assets folder by following the name pattern (example for font Roboto)
```
Normal => fonts/<FontName>-Regular.<Font Extension> (eg:- fonts/Roboto-Regular.ttf)
Bold => fonts/<FontName>-Bold.<Font Extension> (eg:- fonts/Roboto-Bold.ttf)
Italic => fonts/<FontName>-Italic.<Font Extension> (eg:- fonts/Roboto-Italic.ttf)
BoldItalic => fonts/<FontName>-BoldItalic.<Font Extension> (eg:- fonts/Roboto-BoldItalic.ttf)
```
use `fonts/<FontName>.<Font Extension>` ( 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/<FontName>.<Font Extension>` ( eg:- `fonts/Roboto.ttf`) in the assets folder
#FAQ

### Font Resolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -20,6 +24,8 @@ public final class TypefaceUtils {

private static final Map<String, Typeface> sCachedFonts = new HashMap<String, Typeface>();
private static final Map<Typeface, CalligraphyTypefaceSpan> sCachedSpans = new HashMap<Typeface, CalligraphyTypefaceSpan>();
private static final List<String> sMissingFontPaths = new ArrayList<>();
private static final List<String> sValidFontPaths = new ArrayList<>();

/**
* A helper loading a custom font.
Expand Down Expand Up @@ -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;
}
}
}