简体   繁体   English

防止 Jetpack Compose 中的文本在设备字体大小增加时放大

[英]Prevent Text in Jetpack Compose from enlarging when device font size is increases

I have a screen in my app which displays a timer.我的应用程序中有一个显示计时器的屏幕。 If the user decides to increase the font size in the device settings menu then the text becomes too large for the layout and it begins to wrap.如果用户决定在设备设置菜单中增加字体大小,那么文本对于布局来说变得太大并且开始换行。 This is not an issue for my other screens that are more text heavy.对于我的其他文字较多的屏幕来说,这不是问题。 For this screen - and only this screen - I would prefer to prevent the timer text from increasing in size if accessibility options are used.对于这个屏幕 - 并且只有这个屏幕 - 如果使用辅助功能选项,我更愿意防止计时器文本的大小增加。

格式良好的撰写页面 设备字体设置 格式错误的撰写页面

The code in question looks like this, if it adds context:如果添加上下文,则有问题的代码如下所示:

HorizontalPager(state = pagerState, dragEnabled = dragEnabled) { page ->
    val timeInSeconds = abs(steps[page % steps.size] / 1000L)
    val minutes = (timeInSeconds / 60).toString().padStart(2, '0')
    val seconds = (timeInSeconds % 60).toString().padStart(2, '0')

    Text(
        modifier = Modifier.fillMaxWidth(0.85f),
        text = stringResource(R.string.pomodoro_active_notification_content_body, minutes, seconds),
        textAlign = TextAlign.Center,
        fontSize = LocalDimens.current.intervalTimeFontSize,
        style = MaterialTheme.typography.h1
    )
}

As @CommonsWare correctly pointed out, you need to reverse scaling.正如@CommonsWare 正确指出的那样,您需要反向缩放。

You can get fontScale from LocalDensity :您可以从fontScale获得LocalDensity

fontSize = with(LocalDensity.current) {
    (LocalDimens.current.intervalTimeFontSize / fontScale).sp
},
val textStyle = MaterialTheme.typography.h1

val fontSizeDp = with(LocalDensity.current) { textStyle.fontSize.value.dp.toSp() }
Text(
    ...
    style = textStyle,
    fontSize = fontSizeDp,
)

This is an extension function for Int but you can do it for Float if you wish to这是 Int 的扩展功能,但如果您愿意,可以为 Float 执行此功能

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

We can use CompositionLocalProvider to override the default font scale:我们可以使用CompositionLocalProvider来覆盖默认字体比例:

@Composable
fun TextWithFixedSize(
    text: String,
    fontSize: TextUnit,
) {
    val newDensity = Density(
        LocalDensity.current.density,
        fontScale = 2f,
    )
    CompositionLocalProvider(
        LocalDensity provides newDensity,
    ) {
        Text(
            text = text,
            fontSize = fontSize,
        )
    }
}

If you want to use a fixed text size on all the pages of your app, just wrap your root composable with the CompositionLocalProvider.如果您想在应用程序的所有页面上使用固定的文本大小,只需使用 CompositionLocalProvider 包装您的根可组合。

The same app with different font size settings:具有不同字体大小设置的同一应用程序:

![![在此处输入图像描述

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

相关问题 防止系统字体缩放 - Jetpack Compose - Prevent system font scaling - Jetpack Compose 如何防止 Jetpack Compose ExposedDropdownMenuBox 在滚动时显示菜单 - How to prevent Jetpack Compose ExposedDropdownMenuBox from showing menu when scrolling Jetpack Compose,没有字体填充的居中文本? - Jetpack Compose, centering text without font padding? 防止键盘出现在 Jetpack Compose 应用程序中 - Prevent the keyboard from appearing in a Jetpack Compose app 当我以默认设置使用 Jetpack Compose 时,Text("Hello") 如何显示字体颜色? - How does Text("Hello") display font color when I use Jetpack Compose with default settings? 如何在 Android Jetpack Compose 的 TextField 中更改输入字体大小 - How to change the input font size in TextField of Android Jetpack Compose 为什么这个Android设备会放大文字? - Why is this android device enlarging text? 在ImageViews Android中放大图像时如何防止像素混合 - How to prevent pixels from blending when enlarging images in ImageViews Android Jetpack Compose:当按钮尺寸较小时,如何避免按钮文本消失? - Jetpack Compose: How to avoid Button Text disappearing when Button size is small? Android Jetpack Compose Text设置文字大小的方法 - How to set text size in Android Jetpack Compose Text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM