Jetpack Compose:检查文本最大行数

pra*_*rat 2 android android-jetpack-compose

我有一个场景,如果文本最大行数超过 9,我需要显示一个按钮,否则该按钮不应出现。

我尝试查看 Android 开发人员指南,但找不到任何解决方案。

下面是我的代码:

                    Text(
                        //text = item.content,
                        text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
                                "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, " +
                                "when an unknown printer took a galley of type and scrambled it to make a type specimen book. " +
                                "It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software " +
                                "like Aldus PageMaker including versions of Lorem Ipsum.",

                        modifier = Modifier
                            .padding(bottom = 4.dp),
                        fontSize = 16.sp,
                        //maxLines = 6,
                        textAlign = TextAlign.Start,
                        style = MaterialTheme.typography.body1,
                    )

                
Run Code Online (Sandbox Code Playgroud)

dan*_*elp 6

以下对我有用:

Column {
    var textOverflow by remember { mutableStateOf(false) }
    Text(
        modifier = Modifier.size(50.dp),
        text = "1234567891",
        fontSize = 16.sp,
        maxLines = 2,
        onTextLayout = { textLayoutResult ->
            textOverflow = textLayoutResult.hasVisualOverflow
        },
        textAlign = TextAlign.Start,
        style = MaterialTheme.typography.body1,
    )
    if(textOverflow) {
        Button(onClick = {}) {
            Text("Hello")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)