Added in API level 1

TypefaceSpan


open class TypefaceSpan : MetricAffectingSpan, ParcelableSpan
kotlin.Any
   ↳ 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

Public constructors

Constructs a TypefaceSpan from a Typeface.

Constructs a TypefaceSpan from a parcel.

TypefaceSpan(family: String?)

Constructs a TypefaceSpan based on the font family.

Public methods
open Int

open String?

Returns the font family name set in the span.

open Int

open Typeface?

Returns the typeface set in the span.

open String

open Unit

open Unit

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

open Unit
writeToParcel(dest: Parcel, flags: Int)

Flatten this object in to a Parcel.

Inherited functions

Public constructors

TypefaceSpan

Added in API level 28
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

Added in API level 3
TypefaceSpan(src: Parcel)

Constructs a TypefaceSpan from a parcel.

Parameters
src Parcel: This value cannot be null.

TypefaceSpan

Added in API level 1
TypefaceSpan(family: String?)

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

Added in API level 3
open fun describeContents(): Int
Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

getFamily

Added in API level 1
open fun getFamily(): String?

Returns the font family name set in the span.

Return
String? the font family name This value may be null.

getSpanTypeId

Added in API level 3
open fun getSpanTypeId(): Int

getTypeface

Added in API level 28
open fun getTypeface(): Typeface?

Returns the typeface set in the span.

Return
Typeface? the typeface set This value may be null.

toString

Added in API level 1
open fun toString(): String
Return
String a string representation of the object.

updateDrawState

Added in API level 1
open fun updateDrawState(ds: TextPaint): Unit
Parameters
ds TextPaint: This value cannot be null.

updateMeasureState

Added in API level 1
open fun updateMeasureState(paint: TextPaint): Unit

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
textPaint the paint used for drawing the text This value cannot be null.
paint TextPaint: This value cannot be null.

writeToParcel

Added in API level 3
open fun writeToParcel(
    dest: Parcel,
    flags: Int
): Unit

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_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES