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 #170

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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.Gravity;
import android.view.View;
Expand Down Expand Up @@ -894,7 +896,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 @@ -924,6 +932,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
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public class Style {
/** The resource id for the padding for the view content */
final int paddingDimensionResId;

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

/** The file path and 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 @@ -170,6 +176,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 Down Expand Up @@ -197,6 +205,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 @@ -214,6 +224,8 @@ public Builder() {
imageDrawable = null;
imageResId = 0;
imageScaleType = ImageView.ScaleType.FIT_XY;
fontName = null;
fontNameResId = 0;
}

/**
Expand Down Expand Up @@ -246,6 +258,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 @@ -499,6 +513,18 @@ public Builder setPaddingDimensionResId(int paddingResId) {
return this;
}

/** The file path and 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 file path and 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 @@ -531,6 +557,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,66 @@
/*
* Copyright 2013 Simple Finance Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 {@link Spannable} with a custom {@link Typeface}.
*
* @author Tristan Waddington
*/
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(12);

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("%s", typefaceName));

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

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

// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

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

// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}