is it possible to make Text() font size fixed (not...
# compose
p
is it possible to make Text() font size fixed (not scale with font scale)?
1
z
Assuming you have a really good reason for breaking this accessibility feature, I think you could just provide a LocalDensity that always sets the font scale to 1.
p
yeah, I have a valid use case for fixed size, thanks for the concern. I will give that a go, thanks!
I guess it only scales with "display scale", but with "font scale" it should be fixed?
j
Copy code
@Composable
fun FontScaleLimitProvider(maxFontScale: Float, content: @Composable () -> Unit) {
    val localDensity = LocalDensity.current
    if (localDensity.fontScale < maxFontScale) {
        content()
    } else {
        val newDensity = Density(fontScale = maxFontScale, density = localDensity.density)
        CompositionLocalProvider(LocalDensity provides newDensity) {
            content()
        }
    }
}
We do basically the same for really limited use cases and it works fine…
u
I figured the reason why the font was still scaling despite the
CompositionLocalProvider
density. It’s because I was using
dimensionResource(id = <http://R.dimen.xyz|R.dimen.xyz>)
to read the values which internally scales the raw value to TextUnit. To solve this this is what I did:
Copy code
@Composable
private fun getRawDimensionInDp(@DimenRes id: Int): Float {
    val context = LocalContext.current
    val value = TypedValue()
    context.resources.getValue(id, value, true)
    return TypedValue.complexToFloat(value.data)
}

@Composable
private fun getResolvedFontSize(@DimenRes id: Int): TextUnit {
    getRawDimensionInDp(id = id).sp
}
Here I am reading the value as it is, meaning that 12.dp from dimens.xml will be read as
12.0
and then I convert it to 12.0.sp which enforces the scaling.