Whether I use androidx.compose.foundation.text.BasicText or androidx.compose.material.Text, if there isn't enough space for a text it wraps to the next line, for example:
@Composable
fun EllipsisExample() {
Box(modifier = Modifier.width(160.dp)) {
Text("Lorem ipsum dolor sit amet.")
}
}
renders:
How could I force it to be a single line and draw ellipsis at the end of it? The result I want in this case is something like:
Lorem ipsum dolor s…
Both BasicText and Text have overflow
and maxLines
arguments which can help you.
Text(myText, maxLines = 1, overflow = TextOverflow.Ellipsis)
Here's a full single-line example:
import androidx.compose.material.Text
import androidx.compose.ui.text.style.TextOverflow
@Composable
fun EllipsisExample() {
Box(modifier = Modifier.width(160.dp)) {
Text(
text = "Lorem ipsum dolor sit amet.",
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
Of course you can tune maxLines
to fit your needs:
Text(
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
maxLines = 2,
overflow = TextOverflow.Ellipsis
)