ResourceFont

Class
Common
class ResourceFont
internal constructor(
    val resId: Int,
    override val weight: FontWeight = FontWeight.Normal,
    override val style: FontStyle = FontStyle.Normal,
    val variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style),
    loadingStrategy: FontLoadingStrategy = FontLoadingStrategy.Async,
) : Font

Defines a font to be used while rendering text with resource ID.

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.TextStyle.
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.TextStyle.
loadingStrategy Load strategy for this font

Functions

fun copy(
        resId: Int = this.resId,
        weight: FontWeight = this.weight,
        style: FontStyle = this.style,
    ): ResourceFont
@ExperimentalTextApi
    fun copy(
        resId: Int = this.resId,
        weight: FontWeight = this.weight,
        style: FontStyle = this.style,
        loadingStrategy: FontLoadingStrategy = this.loadingStrategy,
        variationSettings: FontVariation.Settings = this.variationSettings,
    ): ResourceFont

Code Examples

CustomFontFamilySample

@Composable
fun CustomFontFamilySample() {
    val fontFamily =
        FontFamily(
            Font(
                resId = R.font.my_font_400_regular,
                weight = FontWeight.W400,
                style = FontStyle.Normal,
            ),
            Font(
                resId = R.font.my_font_400_italic,
                weight = FontWeight.W400,
                style = FontStyle.Italic,
            ),
        )
    Text(text = "Demo Text", fontFamily = fontFamily)
}