android - Compose Text wrapping to next line but width not reduced - Stack Overflow

I am working on Jetpack Compose UI in Android.I have a following Box which shows a Text.@Preview@Com

I am working on Jetpack Compose UI in Android. I have a following Box which shows a Text.

@Preview
@Composable
fun MyText(modifier: Modifier = Modifier)
{
    Box(modifier = Modifier.heightIn(max = 94.dp).widthIn(max = 270.dp)
        .clip(shape = RoundedCornerShape(20.dp))
        .border(
             width = 1.dp,
             color = Color.Green,
             shape = RoundedCornerShape(20.dp)
        )
        .padding(horizontal = 20.dp, vertical = 8.dp)) {
        Text(
            text = "Learn how to use Compose effectively",
            overflow = TextOverflow.Ellipsis,
            maxLines = 3,
            fontSize = 15.sp,
            fontWeight = FontWeight.W400,
            color = Color.Red
        )
    }
}

It produces attached output. As you can see, "effectively" shifts to next line due to width constraints but it leaves much empty space after "Compose" in the first line. Is there a way to remove that extra left space so that overall width reduces?

I am working on Jetpack Compose UI in Android. I have a following Box which shows a Text.

@Preview
@Composable
fun MyText(modifier: Modifier = Modifier)
{
    Box(modifier = Modifier.heightIn(max = 94.dp).widthIn(max = 270.dp)
        .clip(shape = RoundedCornerShape(20.dp))
        .border(
             width = 1.dp,
             color = Color.Green,
             shape = RoundedCornerShape(20.dp)
        )
        .padding(horizontal = 20.dp, vertical = 8.dp)) {
        Text(
            text = "Learn how to use Compose effectively",
            overflow = TextOverflow.Ellipsis,
            maxLines = 3,
            fontSize = 15.sp,
            fontWeight = FontWeight.W400,
            color = Color.Red
        )
    }
}

It produces attached output. As you can see, "effectively" shifts to next line due to width constraints but it leaves much empty space after "Compose" in the first line. Is there a way to remove that extra left space so that overall width reduces?

Share Improve this question edited 16 hours ago Vivek Mangal asked 16 hours ago Vivek MangalVivek Mangal 6763 gold badges9 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can try to use the onTextLayout callback to find out how long the longest line of the actual text is. Then, set the maximum width of the Text to the longest line length (rounded):

@Preview
@Composable
fun MyText(modifier: Modifier = Modifier) {

    val localDensity = LocalDensity.current
    var textWidth by remember { mutableIntStateOf(Int.MAX_VALUE) }

    Box(
        modifier = Modifier
            .wrapContentWidth()
            .clip(shape = RoundedCornerShape(20.dp))
            .border(
                width = 1.dp,
                color = Color.Green,
                shape = RoundedCornerShape(20.dp)
            )
            .padding(horizontal = 20.dp, vertical = 8.dp)
    ) {

        Text(
            modifier = Modifier
                .requiredHeightIn(max = 94.dp)
                .requiredWidthIn( max =
                    min(
                        localDensity.run { textWidth.toDp() },
                        230.dp
                    )
                ),
            text = "Learn how to use Compose effectively",
            onTextLayout = { textLayoutResult ->
                var longestLine = 0f
                repeat(textLayoutResult.lineCount) { lineIndex ->
                    longestLine = max(longestLine, textLayoutResult.getLineRight(lineIndex))
                }
                textWidth = ceil(longestLine).toInt()
            },
            overflow = TextOverflow.Ellipsis,
            maxLines = 3,
            fontSize = 15.sp,
            fontWeight = FontWeight.W400,
            color = Color.Red
        )
    }
}

Note that I shifted all the Modifiers to the Text Composable and also adjusted the maximum width to not include the padding.

Output:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743757677a4502054.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信