# Typeface.Builder

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

# Builder

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/graphics/Typeface.Builder "View this page in Java")  

```
class Builder
```

|---|------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                         ||
| ↳ | [android.graphics.Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#) |

A builder class for creating new Typeface instance.

Examples, 1) Create Typeface from ttf file.  

```kotlin
<code>
  Typeface.Builder builder = new Typeface.Builder("your_font_file.ttf");
  Typeface typeface = builder.build();
  </code>
```
2) Create Typeface from ttc file in assets directory.  

```kotlin
<code>
  Typeface.Builder builder = new Typeface.Builder(getAssets(), "your_font_file.ttc");
  builder.setTtcIndex(2);  // Set index of font collection.
  Typeface typeface = builder.build();
  </code>
```
3) Create Typeface with variation settings.  

```kotlin
<code>
  Typeface.Builder builder = new Typeface.Builder("your_font_file.ttf");
  builder.setFontVariationSettings("'wght' 700, 'slnt' 20, 'ital' 1");
  builder.setWeight(700);  // Tell the system that this is a bold font.
  builder.setItalic(true);  // Tell the system that this is an italic style font.
  Typeface typeface = builder.build();
  </code>
```

<br />

## Summary

|                                                                                                                                                                                                                        Public constructors                                                                                                                                                                                                                         ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#Builder(android.content.res.AssetManager,%20kotlin.String))`(`assetManager:` `[AssetManager](https://developer.android.com/reference/kotlin/android/content/res/AssetManager.html#)`, `path:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Constructs a builder from an asset manager and a file path in an asset directory. |
| [Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#Builder(java.io.File))`(`path:` `[File](https://developer.android.com/reference/kotlin/java/io/File.html#)`)` Constructs a builder with a file path.                                                                                                                                                                                                                 |
| [Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#Builder(java.io.FileDescriptor))`(`fd:` `[FileDescriptor](https://developer.android.com/reference/kotlin/java/io/FileDescriptor.html#)`)` Constructs a builder with a file descriptor.                                                                                                                                                                               |
| [Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#Builder(kotlin.String))`(`path:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Constructs a builder with a file path.                                                                                                                                                                                                         |

|                                                                                                                                                                                                                                             Public methods                                                                                                                                                                                                                                              ||
|--------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Typeface](https://developer.android.com/reference/kotlin/android/graphics/Typeface)!                  | [build](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#build())`()` Generates new Typeface from specified configuration.                                                                                                                                                                                                                                      |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setFallback](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setFallback(kotlin.String))`(`familyName:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?`)` Sets a fallback family name.                                                                                                                                    |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setFontVariationSettings](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setFontVariationSettings(kotlin.Array))`(`axes:` `[Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)<[FontVariationAxis](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontVariationAxis)!>?`)` Sets a font variation settings. |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setFontVariationSettings](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setFontVariationSettings(kotlin.String))`(`variationSettings:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?`)` Sets a font variation settings.                                                                                                |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setItalic](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setItalic(kotlin.Boolean))`(`italic:` `[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)`)` Sets italic information of the font.                                                                                                                                  |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setTtcIndex](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setTtcIndex(kotlin.Int))`(`ttcIndex:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets an index of the font collection.                                                                                                                                       |
| [Typeface.Builder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#)! | [setWeight](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setWeight(kotlin.Int))`(`weight:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Sets weight of the font.                                                                                                                                                          |

## Public constructors

### Builder

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

```
Builder(
    assetManager: AssetManager, 
    path: String)
```

Constructs a builder from an asset manager and a file path in an asset directory.

|                                                                                     Parameters                                                                                     ||
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `assetManager` | [AssetManager](https://developer.android.com/reference/kotlin/android/content/res/AssetManager.html#):The application's asset manager This value cannot be`null`.  |
| `path`         | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The file name of the font data in the asset directory This value cannot be`null`. |

### Builder

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

```
Builder(path: File)
```

Constructs a builder with a file path.

|                                                                       Parameters                                                                       ||
|--------|------------------------------------------------------------------------------------------------------------------------------------------------|
| `path` | [File](https://developer.android.com/reference/kotlin/java/io/File.html#):The file object refers to the font file. This value cannot be`null`. |

### Builder

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

```
Builder(fd: FileDescriptor)
```

Constructs a builder with a file descriptor. Caller is responsible for closing the passed file descriptor after[build](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#build())is called.

|                                                                                      Parameters                                                                                       ||
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `fd` | [FileDescriptor](https://developer.android.com/reference/kotlin/java/io/FileDescriptor.html#):The file descriptor. The passed fd must be mmap-able. This value cannot be`null`. |

### Builder

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

```
Builder(path: String)
```

Constructs a builder with a file path.

|                                                                      Parameters                                                                      ||
|--------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `path` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):The full path to the font file. This value cannot be`null`. |

## Public methods

### build

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

```
fun build(): Typeface!
```

Generates new Typeface from specified configuration.

|                                                                             Return                                                                             ||
|---------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
| [Typeface](https://developer.android.com/reference/kotlin/android/graphics/Typeface)! | Newly created Typeface. May return null if some parameters are invalid. |

### setFallback

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

```
fun setFallback(familyName: String?): Typeface.Builder!
```

Sets a fallback family name. By specifying a fallback family name, a fallback Typeface will be returned if the[build](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#build())method fails to create a Typeface from the provided font. The fallback family will be resolved with the provided weight and italic information specified by[setWeight](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setWeight(kotlin.Int))and[setItalic](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setItalic(kotlin.Boolean)). If[setWeight](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setWeight(kotlin.Int))is not called, the fallback family keeps the default weight. Similarly, if[setItalic](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setItalic(kotlin.Boolean))is not called, the fallback family keeps the default italic information. For example, calling`builder.setFallback("sans-serif-light")`is equivalent to calling`builder.setFallback("sans-serif").setWeight(300)`in terms of fallback. The default weight and italic information are overridden by calling[setWeight](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setWeight(kotlin.Int))and[setItalic](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setItalic(kotlin.Boolean)). For example, if a Typeface is constructed using`builder.setFallback("sans-serif-light").setWeight(700)`, the fallback text will render as sans serif bold.

|                                                                                                                                                                                           Parameters                                                                                                                                                                                            ||
|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `familyName` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?:A family name to be used for fallback if the provided font can not be used. By passing`null`, build() returns`null`. If[setFallback](https://developer.android.com/reference/kotlin/android/graphics/Typeface.Builder#setFallback(kotlin.String))is not called on the builder,`null`is assumed. |

### setFontVariationSettings

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

```
fun setFontVariationSettings(axes: Array<FontVariationAxis!>?): Typeface.Builder!
```

Sets a font variation settings.

|                                                                                                                                     Parameters                                                                                                                                     ||
|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `axes` | [Array](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/index.html)\<[FontVariationAxis](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontVariationAxis)!\>?:An array of font variation axis tag-value pairs. This value may be`null`. |

### setFontVariationSettings

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

```
fun setFontVariationSettings(variationSettings: String?): Typeface.Builder!
```

Sets a font variation settings.

|                                                                                                                                                 Parameters                                                                                                                                                  ||
|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `variationSettings` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?:See[android.widget.TextView#setFontVariationSettings](https://developer.android.com/reference/kotlin/android/widget/TextView.html#setFontVariationSettings(kotlin.String)). This value may be`null`. |

|                                              Exceptions                                              ||
|--------------------------------------|----------------------------------------------------------------|
| `java.lang.IllegalArgumentException` | If given string is not a valid font variation settings format. |

### setItalic

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

```
fun setItalic(italic: Boolean): Typeface.Builder!
```

Sets italic information of the font. Tells the system the style of the given font. If not provided, the system will resolve the style by reading font tables.

|                                                                 Parameters                                                                  ||
|----------|-----------------------------------------------------------------------------------------------------------------------------------|
| `italic` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html):`true`if the font is italic. Otherwise`false`. |

### setTtcIndex

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

```
fun setTtcIndex(ttcIndex: Int): Typeface.Builder!
```

Sets an index of the font collection. See[android.R.attr#ttcIndex](https://developer.android.com/reference/kotlin/android/R.attr.html#ttcIndex:kotlin.Int). Can not be used for Typeface source. build() method will return null for invalid index.

|                                                                                                           Parameters                                                                                                           ||
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `ttcIndex` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):An index of the font collection. If the font source is not font collection, do not call this method or specify 0. Value is 0 or greater |

### setWeight

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

```
fun setWeight(weight: Int): Typeface.Builder!
```

Sets weight of the font. Tells the system the weight of the given font. If not provided, the system will resolve the weight value by reading font tables.

|                                                                 Parameters                                                                 ||
|----------|----------------------------------------------------------------------------------------------------------------------------------|
| `weight` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html):a weight value. Value is between 1 and 1000 inclusive |