Font
Deprecated Maintained for binary compatibility until Compose 1.3.
fun Font(
resId: Int,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
): Font
Creates a Font with using resource ID.
By default, this will load fonts using FontLoadingStrategy.Blocking
, which blocks the first
frame they are used until the font is loaded. This is the correct behavior for small fonts
available locally.
Fonts made with this factory are local fonts, and will block the first frame for loading. To
allow async font loading use Font(resId, weight, style, isLocal)
Parameters
resId | The resource ID of the font file in font resources. i.e. "R.font.myfont". |
weight | The weight of the font. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
style | The style of the font, normal or italic. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
fun Font(
resId: Int,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
loadingStrategy: FontLoadingStrategy = FontLoadingStrategy.Blocking,
): Font
Creates a Font with using resource ID.
Allows control over FontLoadingStrategy
strategy. You may supply
FontLoadingStrategy.Blocking
, or FontLoadingStrategy.OptionalLocal
for fonts that are
expected on the first frame.
FontLoadingStrategy.Async
, will load the font in the background and cause text reflow when
loading completes. Fonts loaded from a remote source via resources should use
FontLoadingStrategy.Async
.
Parameters
resId | The resource ID of the font file in font resources. i.e. "R.font.myfont". |
weight | The weight of the font. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
style | The style of the font, normal or italic. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
loadingStrategy | Load strategy for this font, may be async for async resource fonts |
@ExperimentalTextApi
fun Font(
resId: Int,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
loadingStrategy: FontLoadingStrategy = FontLoadingStrategy.Blocking,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style),
): Font
fun Font(
familyName: DeviceFontFamilyName,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(),
): Font
Describes a system-installed font that may be present on some Android devices.
You should assume this will not resolve on some devices and provide an appropriate fallback font.
Family name lookup is device and platform-specific, and different OEMs may install different
fonts. All fonts described this way are considered FontLoadingStrategy.OptionalLocal
and will
continue to the next font in the chain if they are not present on a device.
Use this method to prefer locally pre-loaded system fonts when they are available. System fonts are always more efficient to load than reading a font file, or downloadable fonts.
A system installed font resolution will never trigger text reflow.
This descriptor will trust the weight
and style
parameters as accurate. However, it is not
required that the loaded fonts actually support the requested weight and style and this may
trigger platform level font-synthesis of fake bold or fake italic during font resolution.
This Font can not describe the system-installed Typeface.DEFAULT
. All other system-installed
fonts are allowed.
Note: When setting variationSettings
any unset axis may be reset to the font default, ignoring
any axis restrictions in fonts.xml
or font_customizations.xml
. This may have surprising
side-effects when named fonts differ only by the default axis settings in XML. When setting
variation axis for device fonts, ensure you set all possible settings for the font.
Parameters
familyName | Android system-installed font family name |
weight | weight to load |
style | style to load |
variationSettings | font variation settings, unset by default to load default VF from system |
fun Font(
path: String,
assetManager: AssetManager,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style),
): Font
Create a Font declaration from a file in the assets directory. The content of the File
is read
during construction.
Parameters
path | full path starting from the assets directory (i.e. dir/myfont.ttf for assets/dir/myfont.ttf). |
assetManager | Android AssetManager |
weight | The weight of the font. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
style | The style of the font, normal or italic. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
variationSettings | on API 26 and above these settings are applied to a variable font when the font is loaded |
fun Font(
file: File,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style),
): Font
Create a Font declaration from a file. The content of the File
is read during construction.
Parameters
file | the font file. |
weight | The weight of the font. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
style | The style of the font, normal or italic. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
variationSettings | on API 26 and above these settings are applied to a variable font when the font is loaded |
@RequiresApi(26)
fun Font(
fileDescriptor: ParcelFileDescriptor,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style),
): Font
Create a Font declaration from a ParcelFileDescriptor
. The content of the
ParcelFileDescriptor
is read during construction.
Parameters
fileDescriptor | the file descriptor for the font file. |
weight | The weight of the font. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
style | The style of the font, normal or italic. The system uses this to match a font to a font request that is given in a androidx.compose.ui.text.SpanStyle . |
variationSettings | these settings are applied to a variable font when the font is loaded |