# Print output for @column tags ?> TypefaceSpan - Android SDK | Android Developers

Most visited

Recently visited

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(java.lang.String) is used, the previous style of the TextView is kept. When TypefaceSpan(android.graphics.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

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

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 cannot be null.

TypefaceSpan

public TypefaceSpan (Parcel src)

Constructs a TypefaceSpan from a parcel.

Parameters
src Parcel: This value cannot be null.

Public methods

describeContents

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(android.os.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. Value is either 0 or CONTENTS_FILE_DESCRIPTOR

getFamily

public String getFamily ()

Returns the font family name set in the span.

Returns
String the font family name This value may be null.

See also:

getSpanTypeId

public int getSpanTypeId ()

Return a special type identifier for this span class.

Returns
int

getTypeface

public Typeface getTypeface ()

Returns the typeface set in the span.

Returns
Typeface the typeface set This value may be null.

See also:

updateDrawState

public void updateDrawState (TextPaint ds)

Parameters
ds TextPaint: This value cannot be null.

updateMeasureState

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: This value cannot be null.

writeToParcel

public void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of Parcelable.PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES