Created
August 3, 2017 09:35
-
-
Save chiemy/3ad18a6e6d8ff44cfea22ec365a70d8c to your computer and use it in GitHub Desktop.
Custom font span
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
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