Skip to content

Instantly share code, notes, and snippets.

@chiemy
Created August 3, 2017 09: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 chiemy/3ad18a6e6d8ff44cfea22ec365a70d8c to your computer and use it in GitHub Desktop.
Save chiemy/3ad18a6e6d8ff44cfea22ec365a70d8c to your computer and use it in GitHub Desktop.
Custom font span
public class CustomTypefaceSpan extends MetricAffectingSpan {
private final Typeface mTypeface;
public CustomTypefaceSpan(final Typeface typeface) {
this.mTypeface = typeface;
}
@Override
public void updateDrawState(final TextPaint drawState) {
apply(drawState);
}
@Override
public void updateMeasureState(final TextPaint paint) {
apply(paint);
}
private void apply(final Paint paint) {
final Typeface oldTypeface = paint.getTypeface();
final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
final int fakeStyle = oldStyle & ~mTypeface.getStyle();
if ((fakeStyle & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fakeStyle & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(mTypeface);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment