Skip to content

Instantly share code, notes, and snippets.

@ok3141
Created May 7, 2019 09:13
Show Gist options
  • Save ok3141/583717be184f3aa8ed26cf3a673d5eae to your computer and use it in GitHub Desktop.
Save ok3141/583717be184f3aa8ed26cf3a673d5eae to your computer and use it in GitHub Desktop.
CustomTypefaceSpan
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
/**
* @see android.text.style.TypefaceSpan
*/
public class CustomTypefaceSpan extends MetricAffectingSpan {
@Nullable
private Typeface typeface;
@Nullable
private Float textSize;
@Nullable
private Float letterSpacing;
@Nullable
private Integer alpha;
@NonNull
public CustomTypefaceSpan setTypeface(@Nullable Typeface typeface) {
this.typeface = typeface;
return this;
}
@NonNull
public CustomTypefaceSpan setTextSize(@Nullable Float textSize) {
this.textSize = textSize;
return this;
}
@NonNull
public CustomTypefaceSpan setLetterSpacing(@Nullable Float letterSpacing) {
this.letterSpacing = letterSpacing;
return this;
}
@NonNull
public CustomTypefaceSpan setAlpha(@Nullable Integer alpha) {
this.alpha = alpha;
return this;
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
updateTypeface(ds);
}
@Override
public void updateMeasureState(@NonNull TextPaint paint) {
updateTypeface(paint);
}
private void updateTypeface(@NonNull Paint paint) {
if (typeface != null) {
paint.setTypeface(typeface);
}
if (textSize != null) {
paint.setTextSize(textSize);
}
if (letterSpacing != null) {
paint.setLetterSpacing(letterSpacing);
}
if (alpha != null) {
paint.setAlpha(alpha);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment