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

Add custom font support #113

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 20 additions & 3 deletions library/src/de/keyboardsurfer/android/widget/crouton/Crouton.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -679,11 +681,11 @@ public static String getLicenseText() {
boolean isShowing() {
return (null != activity) && (isCroutonViewNotNull() || isCustomViewNotNull());
}

private boolean isCroutonViewNotNull() {
return (null != croutonView) && (null != croutonView.getParent());
}

private boolean isCustomViewNotNull() {
return (null != customView) && (null != customView.getParent());
}
Expand Down Expand Up @@ -854,7 +856,13 @@ private RelativeLayout initializeContentView(final Resources resources) {
private TextView initializeTextView(final Resources resources) {
TextView text = new TextView(this.activity);
text.setId(TEXT_ID);
text.setText(this.text);
if (this.style.fontName != null) {
setTextWithCustomFont(text, this.style.fontName);
} else if (this.style.fontNameResId != 0) {
setTextWithCustomFont(text, resources.getString(this.style.fontNameResId));
} else {
text.setText(this.text);
}
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setGravity(this.style.gravity);

Expand Down Expand Up @@ -882,6 +890,15 @@ private TextView initializeTextView(final Resources resources) {
return text;
}

private void setTextWithCustomFont(TextView text, String fontName) {
if (this.text != null) {
SpannableString s = new SpannableString(this.text);
s.setSpan(new TypefaceSpan(text.getContext(), fontName), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setText(s);
}
}

private void initializeTextViewShadow(final Resources resources, final TextView text) {
int textShadowColor = resources.getColor(this.style.textShadowColorResId);
float textShadowRadius = this.style.textShadowRadius;
Expand Down
28 changes: 28 additions & 0 deletions library/src/de/keyboardsurfer/android/widget/crouton/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public class Style {
/** The resource id for the padding for the view content */
final int paddingDimensionResId;

/** The font name for the view content */
final String fontName;

/** The font name resource id for the view content */
final int fontNameResId;

private Style(final Builder builder) {
this.configuration = builder.configuration;
this.backgroundColorResourceId = builder.backgroundColorResourceId;
Expand All @@ -161,6 +167,8 @@ private Style(final Builder builder) {
this.paddingInPixels = builder.paddingInPixels;
this.paddingDimensionResId = builder.paddingDimensionResId;
this.backgroundColorValue = builder.backgroundColorValue;
this.fontName = builder.fontName;
this.fontNameResId = builder.fontNameResId;
}

/** Builder for the {@link Style} object. */
Expand All @@ -187,6 +195,8 @@ public static class Builder {
private ImageView.ScaleType imageScaleType;
private int paddingInPixels;
private int paddingDimensionResId;
private String fontName;
private int fontNameResId;

/** Creates a {@link Builder} to build a {@link Style} upon. */
public Builder() {
Expand All @@ -203,6 +213,8 @@ public Builder() {
imageDrawable = null;
imageResId = 0;
imageScaleType = ImageView.ScaleType.FIT_XY;
fontName = null;
fontNameResId = 0;
}

/**
Expand Down Expand Up @@ -234,6 +246,8 @@ public Builder(final Style baseStyle) {
imageScaleType = baseStyle.imageScaleType;
paddingInPixels = baseStyle.paddingInPixels;
paddingDimensionResId = baseStyle.paddingDimensionResId;
fontName = baseStyle.fontName;
fontNameResId = baseStyle.fontNameResId;
}
/**
* Set the {@link Configuration} option of the {@link Crouton}.
Expand Down Expand Up @@ -474,6 +488,18 @@ public Builder setPaddingDimensionResId(int paddingResId) {
return this;
}

/** The name of the font for the crouton view's content. */
public Builder setFontName(String fontName) {
this.fontName = fontName;
return this;
}

/** The resource id for the name of the font for the crouton view's content. */
public Builder setFontNameResId(int fontNameResId) {
this.fontNameResId = fontNameResId;
return this;
}

/** @return a configured {@link Style} object. */
public Style build() {
return new Style(this);
Expand Down Expand Up @@ -505,6 +531,8 @@ public String toString() {
", textAppearanceResId=" + textAppearanceResId +
", paddingInPixels=" + paddingInPixels +
", paddingDimensionResId=" + paddingDimensionResId +
", fontName=" + fontName +
", fontNameResId=" + fontNameResId +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.keyboardsurfer.android.widget.crouton;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.util.LruCache;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;

/**
* Style a spannable with a custom {@link Typeface}.
*/
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(5);

private Typeface mTypeface;

/**
* Load the {@link Typeface} and apply to a spannable.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);

if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));

// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}

@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
}

@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
}
}