Created
May 21, 2014 14:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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