|
| 1 | +package com.devspark.robototextview.style; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Typeface; |
| 5 | +import android.text.TextPaint; |
| 6 | +import android.text.style.MetricAffectingSpan; |
| 7 | + |
| 8 | +import com.devspark.robototextview.util.RobotoTextViewUtils; |
| 9 | +import com.devspark.robototextview.util.RobotoTypefaceManager; |
| 10 | + |
| 11 | +/** |
| 12 | + * Span for replacing typeface |
| 13 | + */ |
| 14 | +public class RobotoTypefaceSpan extends MetricAffectingSpan { |
| 15 | + |
| 16 | + /** |
| 17 | + * Created typefaces. |
| 18 | + */ |
| 19 | + private final Typeface mTypeface; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor to use with default typeface (regular). |
| 23 | + * |
| 24 | + * @param context The Context the span is using in, through which it can access the current theme, resources, etc. |
| 25 | + */ |
| 26 | + public RobotoTypefaceSpan(Context context) { |
| 27 | + this(context, RobotoTypefaceManager.Typeface.ROBOTO_REGULAR); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor to use with Typeface Id. |
| 32 | + * |
| 33 | + * @param context The Context the span is using in, through which it can access the current theme, resources, etc. |
| 34 | + * @param typefaceId Typeface Id ({@link com.devspark.robototextview.util.RobotoTypefaceManager.Typeface}) |
| 35 | + */ |
| 36 | + public RobotoTypefaceSpan(Context context, int typefaceId) { |
| 37 | + mTypeface = RobotoTypefaceManager.obtainTypeface(context, typefaceId); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructor to use with set of parameters |
| 42 | + * |
| 43 | + * @param context The Context the span is using in, through which it can access the current theme, resources, etc. |
| 44 | + * @param fontFamily The value of "fontFamily" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.FontFamily}) |
| 45 | + * @param textWeight The value of "textWeight" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.TextWeight}) |
| 46 | + * @param textStyle The value of "textStyle" attribute ({@link com.devspark.robototextview.util.RobotoTypefaceManager.TextStyle}) |
| 47 | + */ |
| 48 | + public RobotoTypefaceSpan(Context context, int fontFamily, int textWeight, int textStyle) { |
| 49 | + mTypeface = RobotoTypefaceManager.obtainTypeface(context, fontFamily, textWeight, textStyle); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void updateDrawState(TextPaint ds) { |
| 54 | + RobotoTextViewUtils.setTypeface(ds, mTypeface); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void updateMeasureState(TextPaint paint) { |
| 59 | + RobotoTextViewUtils.setTypeface(paint, mTypeface); |
| 60 | + } |
| 61 | +} |
0 commit comments