dimsuz
07/21/2021, 4:39 PMKirill Grouchnikov
07/21/2021, 4:41 PMGleno
07/21/2021, 6:05 PMdp
conversion, and a maximum font size that our layout supports (something like 40.dp
). I call that SmartScaledText
because CommonSenseScaledText
seems a bit passive aggressive.Gleno
07/21/2021, 6:12 PM@Composable
fun SmartScaledText(
text: String,
style: TextStyle,
modifier: Modifier = Modifier,
maxSize: TextUnit = 40.sp
) {
@Composable
fun adjustTextStyle(style: TextStyle): TextStyle {
@Composable
fun getFontSizeToLimit(unit: TextUnit): TextUnit {
if (unit == TextUnit.Unspecified)
return unit
fun getSystemFontScale(context: Context): Float {
return try {
context.resources.configuration.fontScale
} catch (e: Exception) {
1f
}
}
val ctx = LocalContext.current
val scale = remember { getSystemFontScale(ctx) }
if (scale <= 1f)
return unit
val real = unit.times(scale)
if (real.isSp && real.value > maxSize.value)
return maxSize.times(1f / scale)
return unit
}
val fontSize = getFontSizeToLimit(style.fontSize)
val lineHeight = getFontSizeToLimit(style.lineHeight)
return style.copy(fontSize = fontSize, lineHeight = lineHeight)
}
BasicText(text = text, modifier = modifier, style = adjustTextStyle(style))
}
Something like that.dimsuz
07/21/2021, 9:04 PMdimsuz
07/21/2021, 9:05 PMText
and using it globally. Oh, and TextField
also I guess