Font.Builder


public static final class Font.Builder
extends Object

java.lang.Object
   ↳ android.graphics.fonts.Font.Builder


A builder class for creating new Font.

Summary

Public constructors

Builder(ByteBuffer buffer)

Constructs a builder with a byte buffer.

Builder(File path)

Constructs a builder with a file path.

Builder(ParcelFileDescriptor fd)

Constructs a builder with a file descriptor.

Builder(ParcelFileDescriptor fd, long offset, long size)

Constructs a builder with a file descriptor.

Builder(AssetManager am, String path)

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

Builder(Resources res, int resId)

Constructs a builder from resources.

Builder(Font font)

Constructs a builder from existing Font instance.

Public methods

Font build()

Creates the font based on the configured values.

Font.Builder setFontVariationSettings(String variationSettings)

Sets the font variation settings.

Font.Builder setFontVariationSettings(FontVariationAxis[] axes)

Sets the font variation settings.

Font.Builder setSlant(int slant)

Sets italic information of the font.

Font.Builder setTtcIndex(int ttcIndex)

Sets an index of the font collection.

Font.Builder setWeight(int weight)

Sets weight of the font.

Inherited methods

Public constructors

Builder

Added in API level 29
public Builder (ByteBuffer buffer)

Constructs a builder with a byte buffer. Note that only direct buffer can be used as the source of font data.

Parameters
buffer ByteBuffer: a byte buffer of a font data This value cannot be null.

Builder

Added in API level 29
public Builder (File path)

Constructs a builder with a file path.

Parameters
path File: a file path to the font file This value cannot be null.

Builder

Added in API level 29
public Builder (ParcelFileDescriptor fd)

Constructs a builder with a file descriptor.

Parameters
fd ParcelFileDescriptor: a file descriptor This value cannot be null.

Builder

Added in API level 29
public Builder (ParcelFileDescriptor fd, 
                long offset, 
                long size)

Constructs a builder with a file descriptor.

Parameters
fd ParcelFileDescriptor: a file descriptor This value cannot be null.

offset long: an offset to of the font data in the file Value is 0 or greater

size long: a size of the font data. If -1 is passed, use until end of the file. Value is -1 or greater

Builder

Added in API level 29
public Builder (AssetManager am, 
                String path)

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

Parameters
am 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 29
public Builder (Resources res, 
                int resId)

Constructs a builder from resources. Resource ID must points the font file. XML font can not be used here.

Parameters
res Resources: the resource of this application. This value cannot be null.

resId int: the resource ID of font file.

Builder

Added in API level 31
public Builder (Font font)

Constructs a builder from existing Font instance.

Parameters
font Font: the font instance. This value cannot be null.

Public methods

build

Added in API level 29
public Font build ()

Creates the font based on the configured values.

Returns
Font the Font object This value cannot be null.

Throws
IOException

setFontVariationSettings

Added in API level 29
public Font.Builder setFontVariationSettings (String variationSettings)

Sets the font variation settings.

Parameters
variationSettings String: see FontVariationAxis#fromFontVariationSettings(String) This value may be null.

Returns
Font.Builder this builder This value cannot be null.

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

setFontVariationSettings

Added in API level 29
public Font.Builder setFontVariationSettings (FontVariationAxis[] axes)

Sets the font variation settings.

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

Returns
Font.Builder this builder This value cannot be null.

setSlant

Added in API level 29
public Font.Builder setSlant (int slant)

Sets italic information of the font. Tells the system the style of the given font. If this function is not called, the system will resolve the style by reading font tables. For example, if you want to use italic font as upright font, call setSlant(FontStyle.FONT_SLANT_UPRIGHT) explicitly.

Parameters
slant int: Value is FontStyle.FONT_SLANT_UPRIGHT, or FontStyle.FONT_SLANT_ITALIC

Returns
Font.Builder this builder This value cannot be null.

setTtcIndex

Added in API level 29
public Font.Builder setTtcIndex (int ttcIndex)

Sets an index of the font collection. See R.attr.ttcIndex.

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
Font.Builder this builder This value cannot be null.

setWeight

Added in API level 29
public Font.Builder setWeight (int weight)

Sets weight of the font. Tells the system the weight of the given font. If this function is not called, the system will resolve the weight value by reading font tables. Here are pairs of the common names and their values.

Value Name Android Definition
100 Thin FontStyle#FONT_WEIGHT_THIN
200 Extra Light (Ultra Light) FontStyle#FONT_WEIGHT_EXTRA_LIGHT
300 Light FontStyle#FONT_WEIGHT_LIGHT
400 Normal (Regular) FontStyle#FONT_WEIGHT_NORMAL
500 Medium FontStyle#FONT_WEIGHT_MEDIUM
600 Semi Bold (Demi Bold) FontStyle#FONT_WEIGHT_SEMI_BOLD
700 Bold FontStyle#FONT_WEIGHT_BOLD
800 Extra Bold (Ultra Bold) FontStyle#FONT_WEIGHT_EXTRA_BOLD
900 Black (Heavy) FontStyle#FONT_WEIGHT_BLACK
Parameters
weight int: a weight value Value is between FontStyle.FONT_WEIGHT_MIN and FontStyle.FONT_WEIGHT_MAX inclusive

Returns
Font.Builder this builder This value cannot be null.