TypefaceSpan  |  API reference  |  Android Developers


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

From interface android.os.Parcelable

int CONTENTS_FILE_DESCRIPTOR

Descriptor bit used with describeContents(): indicates that the Parcelable object's flattened representation includes a file descriptor.

int PARCELABLE_WRITE_RETURN_VALUE

Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)".

Public constructors

TypefaceSpan(Typeface typeface)

Constructs a TypefaceSpan from a Typeface.

TypefaceSpan(Parcel src)

Constructs a TypefaceSpan from a parcel.

TypefaceSpan(String family)

Constructs a TypefaceSpan based on the font family.

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.

String toString()

Returns a string representation of the object.

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

From class android.text.style.MetricAffectingSpan

MetricAffectingSpan getUnderlying()

Returns "this" for most MetricAffectingSpans, but for MetricAffectingSpans that were generated by CharacterStyle.wrap(CharacterStyle), returns the underlying MetricAffectingSpan.

abstract void updateMeasureState(TextPaint textPaint)

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

From class android.text.style.CharacterStyle

CharacterStyle getUnderlying()

Returns "this" for most CharacterStyles, but for CharacterStyles that were generated by wrap(CharacterStyle), returns the underlying CharacterStyle.

abstract void updateDrawState(TextPaint tp)
static CharacterStyle wrap(CharacterStyle cs)

A given CharacterStyle can only applied to a single region of a given Spanned.

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

From interface android.text.ParcelableSpan

abstract int getSpanTypeId()

Return a special type identifier for this span class.

From interface android.os.Parcelable

abstract int describeContents()

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

abstract void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Public constructors

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.

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.

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(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

getFamily

public String getFamily ()

Returns the font family name set in the span.

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

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.

toString

public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

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.