Peter Mandeljc
07/12/2021, 7:53 AMZach Klippenstein (he/him) [MOD]
07/12/2021, 3:06 PMPeter Mandeljc
07/12/2021, 6:59 PMPeter Mandeljc
02/16/2023, 11:42 AMJonas
02/16/2023, 11:57 AM@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…Utkarsh Tiwari
02/21/2023, 1:41 AMCompositionLocalProvider
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:
@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.