Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

TypefaceSpan

public class TypefaceSpan
extends MetricAffectingSpan implements ParcelableSpan

java.lang.Object
   ↳ android.text.style.CharacterStyle
     ↳ android.text.style.MetricAffectingSpan
       ↳ android.text.style.TypefaceSpan


Span that updates the typeface of the text it's attached to. The TypefaceSpan can be constructed either based on a font family or based on a Typeface. When TypefaceSpan(String) is used, the previous style of the TextView is kept. When TypefaceSpan(Typeface) is used, the Typeface style replaces the TextView's style.

For example, let's consider a TextView with android:textStyle="italic" and a typeface created based on a font from resources, with a bold style. When applying a TypefaceSpan based the typeface, the text will only keep the bold style, overriding the TextView's textStyle. When applying a TypefaceSpan based on a font family: "monospace", the resulted text will keep the italic style.

 Typeface myTypeface = Typeface.create(ResourcesCompat.getFont(context, R.font.acme),
 Typeface.BOLD);
 SpannableString string = new SpannableString("Text with typeface span.");
 string.setSpan(new TypefaceSpan(myTypeface), 10, 18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 string.setSpan(new TypefaceSpan("monospace"), 19, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
Text with TypefaceSpans constructed based on a font from resource and from a font family.

Summary

Inherited constants

Public constructors

TypefaceSpan(String family)

Constructs a TypefaceSpan based on the font family.

TypefaceSpan(Typeface typeface)

Constructs a TypefaceSpan from a Typeface.

TypefaceSpan(Parcel src)

Constructs a TypefaceSpan from a parcel.

Public methods

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

String getFamily()

Returns the font family name set in the span.

int getSpanTypeId()

Return a special type identifier for this span class.

Typeface getTypeface()

Returns the typeface set in the span.

void updateDrawState(TextPaint ds)
void updateMeasureState(TextPaint paint)

Classes that extend MetricAffectingSpan implement this method to update the text formatting in a way that can change the width or height of characters.

void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Inherited methods

Public constructors

TypefaceSpan

added in API level 1
public TypefaceSpan (String family)

Constructs a TypefaceSpan based on the font family. The previous style of the TextPaint is kept. If the font family is null, the text paint is not modified.

Parameters
family String: The font family for this typeface. Examples include "monospace", "serif", and "sans-serif"

This value may be null.

TypefaceSpan

added in API level 28
public TypefaceSpan (Typeface typeface)

Constructs a TypefaceSpan from a Typeface. The previous style of the TextPaint is overridden and the style of the typeface is used.

Parameters
typeface Typeface: the typeface

This value must never be null.

TypefaceSpan

added in API level 3
public TypefaceSpan (Parcel src)

Constructs a TypefaceSpan from a parcel.

Parameters
src Parcel

This value must never be null.

Public methods

describeContents

added in API level 3
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

getFamily

added in API level 1
public String getFamily ()

Returns the font family name set in the span.

Returns
String the font family name

This value may be null.

getSpanTypeId

added in API level 3
public int getSpanTypeId ()

Return a special type identifier for this span class.

Returns
int

getTypeface

added in API level 28
public Typeface getTypeface ()

Returns the typeface set in the span.

Returns
Typeface the typeface set

This value may be null.

updateDrawState

added in API level 1
public void updateDrawState (TextPaint ds)

Parameters
ds TextPaint

This value must never be null.

updateMeasureState

added in API level 1
public void updateMeasureState (TextPaint paint)

Classes that extend MetricAffectingSpan implement this method to update the text formatting in a way that can change the width or height of characters.

Parameters
paint TextPaint: the paint used for drawing the text

This value must never be null.

writeToParcel

added in API level 3
public void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: The Parcel in which the object should be written.

This value must never be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE.