# TypefaceSpan

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Inherited Constants](https://developer.android.com/reference/android/text/style/TypefaceSpan#inhconstants)\|[Ctors](https://developer.android.com/reference/android/text/style/TypefaceSpan#pubctors)\|[Methods](https://developer.android.com/reference/android/text/style/TypefaceSpan#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/text/style/TypefaceSpan#inhmethods)  

# TypefaceSpan

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/text/style/TypefaceSpan "View this page in Kotlin")\|Java

`
public


class
TypefaceSpan
`  
`


``


``

extends `[MetricAffectingSpan](https://developer.android.com/reference/android/text/style/MetricAffectingSpan)`


``


implements

`[ParcelableSpan](https://developer.android.com/reference/android/text/ParcelableSpan)`


`

|---|---|---|---------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||||
| ↳ | [android.text.style.CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle) |||
|   | ↳ | [android.text.style.MetricAffectingSpan](https://developer.android.com/reference/android/text/style/MetricAffectingSpan) ||
|   |   | ↳ | android.text.style.TypefaceSpan |

<br />

*** ** * ** ***

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)](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(java.lang.String))is used, the previous style of the`TextView`is kept. When[TypefaceSpan(android.graphics.Typeface)](https://developer.android.com/reference/android/text/style/TypefaceSpan#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);
 
```
![](https://developer.android.com/static/reference/android/images/text/style/typefacespan.png)Text with`TypefaceSpan`s constructed based on a font from resource and from a font family.

<br />

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ### Inherited constants                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From interface[android.os.Parcelable](https://developer.android.com/reference/android/os/Parcelable) |-------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `int` | [CONTENTS_FILE_DESCRIPTOR](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR) Descriptor bit used with[describeContents()](https://developer.android.com/reference/android/os/Parcelable#describeContents()): indicates that the Parcelable object's flattened representation includes a file descriptor.                                                                                                                                            | | `int` | [PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE) Flag for use with[writeToParcel(Parcel, int)](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)): 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](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(android.graphics.Typeface))`(`[Typeface](https://developer.android.com/reference/android/graphics/Typeface)` typeface) ` Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan)from a[Typeface](https://developer.android.com/reference/android/graphics/Typeface). |
| ` `[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(android.os.Parcel))`(`[Parcel](https://developer.android.com/reference/android/os/Parcel)` src) ` Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan)from a parcel.                                                                                              |
| ` `[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` family) ` Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan)based on the font family.                                                                                  |

|                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                               ||
|----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` int`                                                                           | ` `[describeContents](https://developer.android.com/reference/android/text/style/TypefaceSpan#describeContents())`() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.                                                                                                                                                              |
| ` `[String](https://developer.android.com/reference/java/lang/String)            | ` `[getFamily](https://developer.android.com/reference/android/text/style/TypefaceSpan#getFamily())`() ` Returns the font family name set in the span.                                                                                                                                                                                                                                      |
| ` int`                                                                           | ` `[getSpanTypeId](https://developer.android.com/reference/android/text/style/TypefaceSpan#getSpanTypeId())`() ` Return a special type identifier for this span class.                                                                                                                                                                                                                      |
| ` `[Typeface](https://developer.android.com/reference/android/graphics/Typeface) | ` `[getTypeface](https://developer.android.com/reference/android/text/style/TypefaceSpan#getTypeface())`() ` Returns the typeface set in the span.                                                                                                                                                                                                                                          |
| ` `[String](https://developer.android.com/reference/java/lang/String)            | ` `[toString](https://developer.android.com/reference/android/text/style/TypefaceSpan#toString())`() ` Returns a string representation of the object.                                                                                                                                                                                                                                       |
| ` void`                                                                          | ` `[updateDrawState](https://developer.android.com/reference/android/text/style/TypefaceSpan#updateDrawState(android.text.TextPaint))`(`[TextPaint](https://developer.android.com/reference/android/text/TextPaint)` ds) ` <br />                                                                                                                                                           |
| ` void`                                                                          | ` `[updateMeasureState](https://developer.android.com/reference/android/text/style/TypefaceSpan#updateMeasureState(android.text.TextPaint))`(`[TextPaint](https://developer.android.com/reference/android/text/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](https://developer.android.com/reference/android/text/style/TypefaceSpan#writeToParcel(android.os.Parcel,%20int))`(`[Parcel](https://developer.android.com/reference/android/os/Parcel)` dest, int flags) ` Flatten this object in to a Parcel.                                                                                                                           |

| ### Inherited methods |
|-----------------------|---|
| From class` `[android.text.style.MetricAffectingSpan](https://developer.android.com/reference/android/text/style/MetricAffectingSpan)` ` |----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[MetricAffectingSpan](https://developer.android.com/reference/android/text/style/MetricAffectingSpan) | ` `[getUnderlying](https://developer.android.com/reference/android/text/style/MetricAffectingSpan#getUnderlying())`() ` Returns "this" for most MetricAffectingSpans, but for MetricAffectingSpans that were generated by[CharacterStyle.wrap(CharacterStyle)](https://developer.android.com/reference/android/text/style/CharacterStyle#wrap(android.text.style.CharacterStyle)), returns the underlying MetricAffectingSpan. | | ` abstract void`                                                                                         | ` `[updateMeasureState](https://developer.android.com/reference/android/text/style/MetricAffectingSpan#updateMeasureState(android.text.TextPaint))`(`[TextPaint](https://developer.android.com/reference/android/text/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](https://developer.android.com/reference/android/text/style/CharacterStyle)` ` |-------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle)        | ` `[getUnderlying](https://developer.android.com/reference/android/text/style/CharacterStyle#getUnderlying())`() ` Returns "this" for most CharacterStyles, but for CharacterStyles that were generated by[wrap(CharacterStyle)](https://developer.android.com/reference/android/text/style/CharacterStyle#wrap(android.text.style.CharacterStyle)), returns the underlying CharacterStyle. | | ` abstract void`                                                                                      | ` `[updateDrawState](https://developer.android.com/reference/android/text/style/CharacterStyle#updateDrawState(android.text.TextPaint))`(`[TextPaint](https://developer.android.com/reference/android/text/TextPaint)` tp) `                                                                                                                                                                | | ` static `[CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle) | ` `[wrap](https://developer.android.com/reference/android/text/style/CharacterStyle#wrap(android.text.style.CharacterStyle))`(`[CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle)` cs) ` A given CharacterStyle can only applied to a single region of a given Spanned.                                                                            | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(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](https://developer.android.com/reference/java/lang/Object#wait(long))`(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](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[android.text.ParcelableSpan](https://developer.android.com/reference/android/text/ParcelableSpan)` ` |-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract int` | ` `[getSpanTypeId](https://developer.android.com/reference/android/text/ParcelableSpan#getSpanTypeId())`() ` Return a special type identifier for this span class. | ||
| From interface` `[android.os.Parcelable](https://developer.android.com/reference/android/os/Parcelable)` ` |------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract int`  | ` `[describeContents](https://developer.android.com/reference/android/os/Parcelable#describeContents())`() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.                                    | | ` abstract void` | ` `[writeToParcel](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int))`(`[Parcel](https://developer.android.com/reference/android/os/Parcel)` dest, int flags) ` Flatten this object in to a Parcel. | ||

## Public constructors

### TypefaceSpan

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public TypefaceSpan (Typeface typeface)
```

Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan)from a[Typeface](https://developer.android.com/reference/android/graphics/Typeface). The previous style of the TextPaint is overridden and the style of the typeface is used.

<br />

|                               Parameters                                ||
|------------|-------------------------------------------------------------|
| `typeface` | `Typeface`: the typeface This value cannot be`null`. <br /> |

### TypefaceSpan

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public TypefaceSpan (Parcel src)
```

Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/TypefaceSpan)from a parcel.

<br />

|                     Parameters                      ||
|-------|----------------------------------------------|
| `src` | `Parcel`: This value cannot be`null`. <br /> |

### TypefaceSpan

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public TypefaceSpan (String family)
```

Constructs a[TypefaceSpan](https://developer.android.com/reference/android/text/style/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.

<br />

|                                                                   Parameters                                                                   ||
|----------|--------------------------------------------------------------------------------------------------------------------------------------|
| `family` | `String`: The font family for this typeface. Examples include "monospace", "serif", and "sans-serif" This value may be`null`. <br /> |

## Public methods

### describeContents

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)), the return value of this method must include the[CONTENTS_FILE_DESCRIPTOR](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR)bit.

<br />

|                                                                                                                         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](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR) <br /> |

### getFamily

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getFamily ()
```

Returns the font family name set in the span.

<br />

|                                                         Returns                                                          ||
|--------------------------------------------------------------------|------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the font family name This value may be`null`. <br /> |

**See also:**

- [TypefaceSpan(String)](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(java.lang.String))  

### getSpanTypeId

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getSpanTypeId ()
```

Return a special type identifier for this span class.

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### getTypeface

Added in[API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Typeface getTypeface ()
```

Returns the typeface set in the span.

<br />

|                                                             Returns                                                             ||
|-------------------------------------------------------------------------------|--------------------------------------------------|
| [Typeface](https://developer.android.com/reference/android/graphics/Typeface) | the typeface set This value may be`null`. <br /> |

**See also:**

- [TypefaceSpan(Typeface)](https://developer.android.com/reference/android/text/style/TypefaceSpan#TypefaceSpan(android.graphics.Typeface))  

### toString

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns a string representation of the object.

<br />

|                                                      Returns                                                      ||
|--------------------------------------------------------------------|-----------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the object. <br /> |

### updateDrawState

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void updateDrawState (TextPaint ds)
```

<br />

<br />

|                      Parameters                       ||
|------|-------------------------------------------------|
| `ds` | `TextPaint`: This value cannot be`null`. <br /> |

### updateMeasureState

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                        Parameters                        ||
|---------|-------------------------------------------------|
| `paint` | `TextPaint`: This value cannot be`null`. <br /> |

### writeToParcel

Added in[API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void writeToParcel (Parcel dest, 
                int flags)
```

Flatten this object in to a Parcel.

<br />

|                                                                                                                                                                                                                               Parameters                                                                                                                                                                                                                                ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dest`  | `Parcel`: This value cannot be`null`. <br />                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or[Parcelable.PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE). Value is either`0`or a combination of[Parcelable.PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE), and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES <br /> |