Reading font size from resource file with Jetpack Compose

Viewed 361

I am trying to implement a requirement defined by the project's design team for different font sizes for phone and tablet. I thought, I could store the values using Android's resource resolution and read them with dimensionResource. To my surprise Compose's TextUnit needs the raw sp value, so I am left with reconverting the resolved value back to sp

val fontSize = with(LocalDensity.current) {
    dimensionResource(id = R.dimen.font_size_h1).toSp()
}

which seems like an unnecessary round trip. Alternatively, I could store the values as integer resources and then build TextUnit with:

@OptIn(ExperimentalUnitApi::class)
TextUnit(integerResource(id = R.integer.font_size_h1).toFloat(), TextUnitType.Sp)

which requires use of an experimental Api.

Is there some other solution, that I am missing?

0 Answers
Related