Skip to content

Most visited

Recently visited

navigation

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 buidler = new Typeface.Builder("your_font_file.ttf");
 Typeface typeface = builder.build();
 
 
2) Create Typeface from ttc file in assets directory.
 
 Typeface.Builder buidler = 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 buidler = 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

Typeface.Builder(File path)

Constructs a builder with a file path.

Typeface.Builder(FileDescriptor fd)

Constructs a builder with a file descriptor.

Typeface.Builder(String path)

Constructs a builder with a file path.

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

From class java.lang.Object

Public constructors

Typeface.Builder

added in API level 26
Typeface.Builder (File path)

Constructs a builder with a file path.

Parameters
path File: The file object refers to the font file.

This value must never be null.

Typeface.Builder

added in API level 26
Typeface.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 must never be null.

Typeface.Builder

added in API level 26
Typeface.Builder (String path)

Constructs a builder with a file path.

Parameters
path String: The full path to the font file.

This value must never be null.

Typeface.Builder

added in API level 26
Typeface.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 must never be null.

path String: The file name of the font data in the asset directory

This value must never be null.

Public methods

build

added in API level 26
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
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. Similary, 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
Typeface.Builder setFontVariationSettings (String variationSettings)

Sets a font variation settings.

Parameters
variationSettings String: See 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
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
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
Typeface.Builder setTtcIndex (int ttcIndex)

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

Returns
Typeface.Builder

setWeight

added in API level 26
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

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)