如何删除 Text Jetpack Compose 中的默认填充

How to remove default padding in Text Jetpack Compose

在android视图中,我们可以通过以下方式添加TextView数据:

android:includeFontPadding="false"

compose 中 includeFontPadding 的替代品是什么?

使用

style = TextStyle(
    platformStyle = PlatformTextStyle(
        includeFontPadding = false,
    ),
),

和 Opt-in 使用 @OptIn(ExperimentalTextApi::class)

注意:PlatformTextStyle 已弃用并显示以下消息。

Enables turning on and off for Android includeFontPadding .

includeFontPadding was added to Android in order to prevent clipping issues on tall scripts. However that issue has been fixed since Android 28. Jetpack Compose backports the fix for Android versions prior to Android 28. Therefore the original reason why includeFontPadding was needed in invalid on Compose.
This configuration was added for migration of the apps in case some code or design was relying includeFontPadding=true behavior and will be removed.

来源:https://issuetracker.google.com/issues/171394808

撰写版本:"1.2.0-beta02"

示例代码和屏幕截图

@OptIn(ExperimentalTextApi::class)
@Composable
fun TextWithoutPadding() {
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .fillMaxSize(),
    ) {
        Text(
            text = AnnotatedString("Sample Text"),
            fontSize = 64.sp,
            style = TextStyle(
                platformStyle = PlatformTextStyle(
                    includeFontPadding = true,
                ),
            ),
            modifier = Modifier
                .background(
                    color = Cyan,
                ),
        )
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = AnnotatedString("Sample Text"),
            fontSize = 64.sp,
            style = TextStyle(
                platformStyle = PlatformTextStyle(
                    includeFontPadding = false,
                ),
            ),
            modifier = Modifier
                .background(
                    color = Cyan,
                ),
        )
    }
}