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 setting Typeface object directly #62

Open
wants to merge 4 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
26 changes: 15 additions & 11 deletions CalligraphySample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ android {

defaultConfig {
applicationId "io.github.inflationx.calligraphy3.sample"
minSdkVersion 14
minSdkVersion 26
targetSdkVersion 28
versionCode project.ext.versionCodeInt
versionName version
}
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
// debug {
// minifyEnabled true
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
// }
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
Expand All @@ -27,16 +27,20 @@ android {
textOutput 'stdout'
textReport System.getenv('CI') == 'true'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation project(':calligraphy')
implementation 'io.github.inflationx:viewpump:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'io.github.inflationx:viewpump:2.0.3'
implementation 'androidx.appcompat:appcompat:1.3.1'

implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

debugImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.inflationx.calligraphy3.CalligraphyConfig;
import io.github.inflationx.calligraphy3.CalligraphyInterceptor;
import io.github.inflationx.calligraphy3.FontMapper;
import io.github.inflationx.calligraphy3.TypefaceUtils;
import io.github.inflationx.viewpump.ViewPump;

/**
Expand All @@ -19,7 +20,8 @@ public void onCreate() {
ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
// .setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
.setDefaultTypeface(TypefaceUtils.load(getAssets(), "fonts/Roboto-ThinItalic.ttf"))
.setFontAttrId(R.attr.fontPath)
.setFontMapper(new FontMapper() {
@Override
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
Expand All @@ -12,7 +12,7 @@ allprojects {
repositories {
google()
mavenLocal()
jcenter()
mavenCentral()
}
// Is Release Build?
version = getProperty('VERSION_NAME')
Expand Down
4 changes: 2 additions & 2 deletions calligraphy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
}

dependencies {
compileOnly 'androidx.appcompat:appcompat:1.0.2'
implementation 'io.github.inflationx:viewpump:1.0.0'
compileOnly 'androidx.appcompat:appcompat:1.3.1'
implementation 'io.github.inflationx:viewpump:2.0.3'

testImplementation 'androidx.annotation:annotation:1.0.0'
testImplementation 'androidx.test:runner:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ void onViewCreatedInternal(View view, final Context context, AttributeSet attrs)
}

private Typeface getDefaultTypeface(Context context, String fontPath) {
if (mCalligraphyConfig.getFontTypeface() != null) {
return mCalligraphyConfig.getFontTypeface();
}
if (TextUtils.isEmpty(fontPath)) {
fontPath = mCalligraphyConfig.getFontPath();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.inflationx.calligraphy3;

import android.graphics.Typeface;
import android.os.Build;

import android.text.TextUtils;
Expand Down Expand Up @@ -81,6 +82,10 @@ private static void addAppCompatViews() {
* The default Font Path if nothing else is setup.
*/
private final String mFontPath;
/**
* The default Font Path if nothing else is setup.
*/
private final Typeface mFontTypeface;
/**
* Default Font Path Attr Id to lookup
*/
Expand All @@ -106,6 +111,7 @@ private static void addAppCompatViews() {
private CalligraphyConfig(Builder builder) {
mIsFontSet = builder.isFontSet;
mFontPath = builder.fontAssetPath;
mFontTypeface = builder.fontTypeface;
mAttrId = builder.attrId;
mCustomViewTypefaceSupport = builder.customViewTypefaceSupport;
final Map<Class<? extends TextView>, Integer> tempMap = new HashMap<>(DEFAULT_STYLES);
Expand All @@ -122,6 +128,13 @@ public String getFontPath() {
return mFontPath;
}

/**
* @return mFontTypeface for text views might be null
*/
public Typeface getFontTypeface() {
return mFontTypeface;
}

/**
* @return true if set, false if null|empty
*/
Expand Down Expand Up @@ -173,6 +186,10 @@ public static class Builder {
* The default fontPath
*/
private String fontAssetPath = null;
/**
* The default Typeface
*/
private Typeface fontTypeface = null;
/**
* Additional Class Styles. Can be empty.
*/
Expand All @@ -198,14 +215,31 @@ public Builder setFontAttrId(int fontAssetAttrId) {
*
* @param defaultFontAssetPath a path to a font file in the assets folder, e.g. "fonts/Roboto-light.ttf",
* passing null will default to the device font-family.
* This clears default font typeface.
* @return this builder.
*/
public Builder setDefaultFontPath(String defaultFontAssetPath) {
this.isFontSet = !TextUtils.isEmpty(defaultFontAssetPath);
this.fontTypeface = null;
this.fontAssetPath = defaultFontAssetPath;
return this;
}

/**
* Set the default font if you don't define one else where in your styles.
*
* @param defaultFontTypeface a Typeface object,
* passing null will default to the device font-family.
* This clears default font asset path.
* @return this builder.
*/
public Builder setDefaultTypeface(Typeface defaultFontTypeface) {
this.isFontSet = defaultFontTypeface != null;
this.fontAssetPath = null;
this.fontTypeface = defaultFontTypeface;
return this;
}

/**
* Add a custom style to get looked up. If you use a custom class that has a parent style
* which is not part of the default android styles you will need to add it here.
Expand Down Expand Up @@ -243,7 +277,7 @@ public Builder setFontMapper(FontMapper fontMapper) {
}

public CalligraphyConfig build() {
this.isFontSet = !TextUtils.isEmpty(fontAssetPath);
this.isFontSet = !TextUtils.isEmpty(fontAssetPath) || this.fontTypeface != null;
return new CalligraphyConfig(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ static boolean applyFontToTextView(final Context context, final TextView textVie
return applyFontToTextView(textView, typeface, deferred);
}

static void applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config) {
applyFontToTextView(context, textView, config, false);
static boolean applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config) {
return applyFontToTextView(context, textView, config, false);
}

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);
static boolean applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, boolean deferred) {
if (textView == null || config == null) return false;
if (!config.isFontSet()) return false;
if (config.getFontTypeface() != null) {
return applyFontToTextView(textView, config.getFontTypeface(), deferred);
} else {
return applyFontToTextView(context, textView, config.getFontPath(), deferred);
}
}

/**
Expand All @@ -134,16 +138,16 @@ static void applyFontToTextView(final Context context, final TextView textView,
* @param textViewFont nullable, will use Default Config if null or fails to find the
* defined font.
*/
public static void applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, final String textViewFont) {
applyFontToTextView(context, textView, config, textViewFont, false);
public static boolean applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, final String textViewFont) {
return applyFontToTextView(context, textView, config, textViewFont, false);
}

static void applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, final String textViewFont, boolean deferred) {
if (context == null || textView == null || config == null) return;
static boolean applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, final String textViewFont, boolean deferred) {
if (context == null || textView == null || config == null) return false;
if (!TextUtils.isEmpty(textViewFont) && applyFontToTextView(context, textView, textViewFont, deferred)) {
return;
return false;
}
applyFontToTextView(context, textView, config, deferred);
return applyFontToTextView(context, textView, config, deferred);
}

/**
Expand Down