Typeface.Builder


public static final class Typeface.Builder
extends Object

java.lang.Object
   ↳ android.graphics.Typeface.Builder


A builder class for creating new Typeface instance.

Examples, 1) Create Typeface from ttf file.

 
 Typeface.Builder builder = new Typeface.Builder("your_font_file.ttf");
 Typeface typeface = builder.build();
 
 
2) Create Typeface from ttc file in assets directory.
 
 Typeface.Builder builder = new Typeface.Builder(getAssets(), "your_font_file.ttc");
 builder.setTtcIndex(2);  // Set index of font collection.
 Typeface typeface = builder.build();
 
 
3) Create Typeface with variation settings.
 
 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();
 
 

Summary

Public constructors

Builder(File path)

Constructs a builder with a file path.

Builder(FileDescriptor fd)

Constructs a builder with a file descriptor.

Builder(String path)

Constructs a builder with a file path.

Builder(AssetManager assetManager, String path)

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

Public methods

Typeface build()

Generates new Typeface from specified configuration.

Typeface.Builder setFallback(String familyName)

Sets a fallback family name.

Typeface.Builder setFontVariationSettings(String variationSettings)

Sets a font variation settings.

Typeface.Builder setFontVariationSettings(FontVariationAxis[] axes)

Sets a font variation settings.

Typeface.Builder setItalic(boolean italic)

Sets italic information of the font.

Typeface.Builder setTtcIndex(int ttcIndex)

Sets an index of the font collection.

Typeface.Builder setWeight(int weight)

Sets weight of the font.

Inherited methods

Public constructors

Builder

Added in API level 26
public Builder (File path)

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
public Builder (FileDescriptor fd)

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
public Builder (String path)

Constructs a builder with a file path.

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

Builder

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

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.

Public methods

build

Added in API level 26
public Typeface build ()

Generates new Typeface from specified configuration.

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

setFallback

Added in API level 26
public Typeface.Builder setFallback (String familyName)

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

Returns
Typeface.Builder

setFontVariationSettings

Added in API level 26
public Typeface.Builder setFontVariationSettings (String variationSettings)

Sets a font variation settings.

Parameters
variationSettings String: See TextView.setFontVariationSettings(String). This value may be null.

Returns
Typeface.Builder

Throws
IllegalArgumentException If given string is not a valid font variation settings format.

setFontVariationSettings

Added in API level 26
public Typeface.Builder setFontVariationSettings (FontVariationAxis[] axes)

Sets a font variation settings.

Parameters
axes FontVariationAxis: An array of font variation axis tag-value pairs. This value may be null.

Returns
Typeface.Builder

setItalic

Added in API level 26
public Typeface.Builder setItalic (boolean italic)

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.

Returns
Typeface.Builder

setTtcIndex

Added in API level 26
public Typeface.Builder setTtcIndex (int ttcIndex)

Sets an index of the font collection. See 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

Returns
Typeface.Builder

setWeight

Added in API level 26
public Typeface.Builder setWeight (int weight)

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

Returns
Typeface.Builder