Added in API level 26

Builder


class Builder
kotlin.Any
   ↳ android.graphics.Typeface.Builder

A builder class for creating new Typeface instance.

Examples, 1) Create Typeface from ttf file.

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

Summary

Public constructors
Builder(assetManager: AssetManager, path: String)

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

Builder(path: File)

Constructs a builder with a file path.

Constructs a builder with a file descriptor.

Builder(path: String)

Constructs a builder with a file path.

Public methods
Typeface!

Generates new Typeface from specified configuration.

Typeface.Builder!
setFallback(familyName: String?)

Sets a fallback family name.

Typeface.Builder!

Sets a font variation settings.

Typeface.Builder!
setFontVariationSettings(variationSettings: String?)

Sets a font variation settings.

Typeface.Builder!
setItalic(italic: Boolean)

Sets italic information of the font.

Typeface.Builder!
setTtcIndex(ttcIndex: Int)

Sets an index of the font collection.

Typeface.Builder!
setWeight(weight: Int)

Sets weight of the font.

Public constructors

Builder

Added in API level 26
Builder(
    assetManager: AssetManager,
    path: String)

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

Parameters
assetManager AssetManager: The application's asset manager This value cannot be null.
path String: The file name of the font data in the asset directory This value cannot be null.

Builder

Added in API level 26
Builder(path: File)

Constructs a builder with a file path.

Parameters
path File: The file object refers to the font file. This value cannot be null.

Builder

Added in API level 26
Builder(fd: FileDescriptor)

Constructs a builder with a file descriptor. Caller is responsible for closing the passed file descriptor after build is called.

Parameters
fd FileDescriptor: The file descriptor. The passed fd must be mmap-able. This value cannot be null.

Builder

Added in API level 26
Builder(path: String)

Constructs a builder with a file path.

Parameters
path String: The full path to the font file. This value cannot be null.

Public methods

build

Added in API level 26
fun build(): Typeface!

Generates new Typeface from specified configuration.

Return
Typeface! Newly created Typeface. May return null if some parameters are invalid.

setFallback

Added in API level 26
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 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 and setItalic. If setWeight is not called, the fallback family keeps the default weight. Similarly, if setItalic 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 and setItalic. 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?: A family name to be used for fallback if the provided font can not be used. By passing null, build() returns null. If setFallback is not called on the builder, null is assumed.

setFontVariationSettings

Added in API level 26
fun setFontVariationSettings(axes: Array<FontVariationAxis!>?): Typeface.Builder!

Sets a font variation settings.

Parameters
axes Array<FontVariationAxis!>?: An array of font variation axis tag-value pairs. This value may be null.

setFontVariationSettings

Added in API level 26
fun setFontVariationSettings(variationSettings: String?): Typeface.Builder!

Sets a font variation settings.

Parameters
variationSettings String?: See android.widget.TextView#setFontVariationSettings. 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
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: true if the font is italic. Otherwise false.

setTtcIndex

Added in API level 26
fun setTtcIndex(ttcIndex: Int): Typeface.Builder!

Sets an index of the font collection. See android.R.attr#ttcIndex. Can not be used for Typeface source. build() method will return null for invalid index.

Parameters
ttcIndex Int: 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
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: a weight value. Value is between 1 and 1000 inclusive