Skip to content

Instantly share code, notes, and snippets.

@bgorkowy
Created May 21, 2014 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgorkowy/0eeaf66151ae856e4a70 to your computer and use it in GitHub Desktop.
Save bgorkowy/0eeaf66151ae856e4a70 to your computer and use it in GitHub Desktop.
CustomSpan, with the ability to set style from resource file and custom typeface. applyCustomTypeface() method taken from http://goo.gl/dtJulZ, TextApperanceSpan from android source code.
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TextAppearanceSpan;
/**
* Created by bgorkowy on 2014-05-21.
*/
public class CustomTypefaceStyleSpan extends TextAppearanceSpan {
private Typeface mNewTypeface;
public CustomTypefaceStyleSpan(Context context, int appearance, Typeface typeface) {
super(context, appearance);
mNewTypeface = typeface;
}
@Override
public void updateDrawState(TextPaint ds) {
updateMeasureState(ds);
if (getTextColor() != null) {
ds.setColor(getTextColor().getColorForState(ds.drawableState, 0));
}
if (getLinkTextColor() != null) {
ds.linkColor = getLinkTextColor().getColorForState(ds.drawableState, 0);
}
}
@Override
public void updateMeasureState(TextPaint ds) {
applyCustomTypeFace(ds, mNewTypeface);
if (getTextSize() > 0) {
ds.setTextSize(getTextSize());
}
}
private static void applyCustomTypeFace(Paint ds, Typeface tf) {
int oldStyle;
Typeface old = ds.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
ds.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
ds.setTextSkewX(-0.25f);
}
ds.setTypeface(tf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment