Created
May 7, 2019 09:13
-
-
Save ok3141/583717be184f3aa8ed26cf3a673d5eae to your computer and use it in GitHub Desktop.
CustomTypefaceSpan
This file contains hidden or 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.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