简体   繁体   English

防止系统字体缩放 - Jetpack Compose

[英]Prevent system font scaling - Jetpack Compose

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.他们中的大多数人告诉使用 dp 而不是 dp 来表示文本大小,但是在撰写中我们只能使用 sp 如果我是正确的,因为它需要一个文本单元。 Is there any right way to restrict font scaling in our app done with jetpack compose?是否有任何正确的方法来限制我们使用 jetpack compose 完成的应用程序中的字体缩放? Please help.请帮忙。

(Solutions refered): https://l.workplace.com/l.php?u=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F21546805%2Fhow-to-prevent-system-font-size-changing-effects-to-android-application&h=AT0zIuBPbUONm0T6q8PtqbxCdX6P_ywlp-yFGrqPMqZt7H3wsWYltKO5XwbW3i0lenrxxLi3nn_kMO4aPtFUfig2iG0BcRZpd0wTuZ1_XFpdsjDM6E7RPyZ-G_c2dlmuzGqsSEHYbqBJun0hLLZgOpRUszKbe9-1xQ (参考解决方案): https://l.workplace.com/l.php?u=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F21546805%2Fhow-to-prevent-system-size-change-to-prevent-system- -to-android-application&h=AT0zIuBPbUONm0T6q8PtqbxCdX6P_ywlp-yFGrqPMqZt7H3wsWYltKO5XwbW3i0lenrxxLi3nn_kMO4aPtFUfig2iG0BcRZpd0wTuZ1_XFpdsjDM6E7RPyZ-G_c2dlmuzGqsSERUbqB90hLL1xQQOgB

You can have an extension for Int or Float like this您可以像这样对IntFloat进行扩展

@Composable
fun Int.scaledSp(): TextUnit {
    val value: Int = this
    return with(LocalDensity.current) {
        val fontScale = this.fontScale
        val textSize =  value / fontScale
        textSize.sp
  }

Till there is no solution on jetpack compose for Text() , you can use AndroidView:直到 jetpack compose for Text()没有解决方案,您可以使用 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.
                    }
                },)

}

This way can be succeed.这种方式可以成功。

@Composable
fun DisableFontScale(content: @Composable () -> Unit) {
    val density = LocalDensity.current.density
    CompositionLocalProvider(
        LocalDensity provides Density(
            density = density,
            fontScale = 1f
        ),
        content = content
    )
}

@Composable
fun EnableFontScale(content: @Composable () -> Unit) {
    val density = LocalDensity.current.density
    val fontScale = LocalContext.current.resources.configuration.fontScale
    CompositionLocalProvider(
        LocalDensity provides Density(
            density = density,
            fontScale = fontScale
        ),
        content = content
    )
}

@Composable
fun Example() {
    Column {
        Text(text = "This will be scaled.")
        DisableFontScale {
            Text(text = "This will not be scaled.")
            EnableFontScale {
                Text(text = "We enable font scale, so this will be scaled.")
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM