Created
April 15, 2015 13:56
-
-
Save filipeuva/c110b8fd842d0c1a21ab to your computer and use it in GitHub Desktop.
Custom spannable for collapsible sub-titles with different typeface\size\color
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
package com.filipeuva.spannables; | |
import android.graphics.Paint; | |
import android.graphics.Typeface; | |
import android.text.TextPaint; | |
import android.text.style.MetricAffectingSpan; | |
/** | |
* Created by Filipe Uva on 4/5/2015. | |
*/ | |
public class CustomTypefaceSpan extends MetricAffectingSpan { | |
private final Typeface newType; | |
private final float textSize; | |
private final int textColor; | |
public CustomTypefaceSpan(Typeface type, float textSize, int colorResId) { | |
newType = type; | |
this.textSize = textSize; | |
this.textColor = colorResId; | |
} | |
@Override | |
public void updateDrawState(final TextPaint drawState) { | |
apply(drawState); | |
} | |
@Override | |
public void updateMeasureState(final TextPaint paint) { | |
apply(paint); | |
} | |
private void apply(final Paint paint) { | |
int oldStyle; | |
Typeface old = paint.getTypeface(); | |
if (old == null) { | |
oldStyle = 0; | |
} else { | |
oldStyle = old.getStyle(); | |
} | |
if (newType != null) { | |
int fake = oldStyle & ~newType.getStyle(); | |
if ((fake & Typeface.BOLD) != 0) { | |
paint.setFakeBoldText(true); | |
} | |
if ((fake & Typeface.ITALIC) != 0) { | |
paint.setTextSkewX(-0.25f); | |
} | |
paint.setTypeface(newType); | |
} | |
paint.setTextSize(textSize); | |
paint.setColor(textColor); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment