I am trying to restric the app from affected fro system font scaling. I had gone through many solutions but none helped. Most of them tell use dp instead of dp for text sizes but in compose we can use only sp if i am right as it expects a Text Unit. Is there any right way to restrict font scaling in our app done with jetpack compose ? Please help .
CodePudding user response:
Till there is no solution on jetpack compose for Text()
, you can use AndroidView:
@Composable
fun CustomText(
// attributes you need to set
){
AndroidView(factory = { context ->
AppCompatTextView(context).apply {
setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25)
setText("")
// other attributes you want to set or other features which is not available in jetpack compose now.
}
},)
}
CodePudding user response:
You can have an extension for Int
or Float
like this
@Composable
fun Int.scaledSp(): TextUnit {
val value: Int = this
return with(LocalDensity.current) {
val fontScale = this.fontScale
val textSize = value / fontScale
textSize.sp
}