简体   繁体   中英

“.(” in Jetpack Compose Text causes newline

In this Jetpack Compose code the result of the first Text composable has the parenthesized text on a new line:

Column {
    Text(text = "2.(0123456789)",
            modifier = Modifier
                    .width(60.dp)
                    .padding(start = 5.dp))
    Spacer(modifier = Modifier . padding(20.dp))
    Text(text = "2.0123456789",
            modifier = Modifier
                    .width(60.dp)
                    .padding(start = 5.dp))
}

在此处输入图像描述

This does not happen if I remove the ".", or if I remove the "(".

The issue is modifier = Modifier.width(60.dp) which applies a fixed width to your Text .

Use Modifier.width(IntrinsicSize.Max) in your Column

Column(Modifier.width(IntrinsicSize.Max)) {
    Text(text = "2.(0123456789)",
        modifier = Modifier
            .padding(start = 5.dp))
    Spacer(modifier = Modifier . padding(20.dp))
    Text(text = "2.0123456789",
        modifier = Modifier
            .padding(start = 5.dp))
}

在此处输入图像描述

If you want to specify a limited width you can use Modifier.requiredWidthIn to specify a width between mindp and maxdp and maxLines = 1 in the Text .

Something like:

Column(Modifier.requiredWidthIn(0.dp , 75.dp)) {
    Text(
        text = "2.(0123456789)",
        modifier = Modifier
            .padding(start = 5.dp),
        maxLines = 1,
        overflow = TextOverflow.Ellipsis

    )
    Spacer(modifier = Modifier.padding(20.dp))
    Text(
        text = "2.0123456789",
        modifier = Modifier
            .padding(start = 5.dp),
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
}

Otherwise you apply the Modifier.requiredWidthIn(0.dp, 75.dp) to the Text components instead of the Column.

Column(Modifier.width(IntrinsicSize.Max)) {
    Text(
        modifier = Modifier
            .requiredWidthIn(0.dp, 75.dp)
        maxLines = 1
    )
    //...
    Text(
        modifier = Modifier
            .requiredWidthIn(0.dp, 75.dp)
        maxLines = 1,
    )
}

The issue ain't with character escape. It is the fact that you have applied a fixed small width and because the order of the modifiers matters, the width is further trimmed by 5dps of your padding

EDIT: I am not exactly sure, but I think it could be because of the fact that Text inherently supports the feature where a textholder would display a single word in one line if it can. When you insert a special character like a period or a bracket, it considers the rest of the string to be a new sentence (grammatically), and observing it's length, calculating that it will not fit in the same line, starts a new line. Then the length is still bigger but there is no option but to start a new line. Hence, the rest of the phrases go all the way to the end, then start on s new line on reaching maximum capacity. Try inserting just a single character after the period or the bracket. I do not think that it will insert a new line since there will be room to insert the entire statement (in this case, just a single character). When I started typing, I thought it was not for sure, but now, I am almost convinced that this is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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