Skip to content

Instantly share code, notes, and snippets.

@alexvanyo
Created April 30, 2024 00:59
Show Gist options
  • Save alexvanyo/757a45f2c141fc08fa87974f9bd4d723 to your computer and use it in GitHub Desktop.
Save alexvanyo/757a45f2c141fc08fa87974f9bd4d723 to your computer and use it in GitHub Desktop.
@Composable
@Preview(widthDp = 400, heightDp = 600)
@Preview(widthDp = 400, heightDp = 500)
@Preview(widthDp = 400, heightDp = 450)
@Preview(widthDp = 400, heightDp = 400)
@Preview(widthDp = 400, heightDp = 350)
@Preview(widthDp = 400, heightDp = 300)
private fun TextTruncation() {
Surface {
Layout(
content = {
Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.titleSmall,
)
Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.bodyMedium,
)
Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.bodySmall,
)
},
measurePolicy = { measurables, constraints ->
val minimumIntrinsicSizes = measurables
.map { it.minIntrinsicHeight(constraints.maxWidth) }
// Determine how much space would be need to fit everything up to a certain point at the minimum
// intrinsic size.
// Index n of this list refers to the required space to fit measurables 0 to n (inclusive) and
// fulfill their intrinsic minimum size
val heightsNeededForMinimumIntrinsicSizes = minimumIntrinsicSizes.runningReduce(Int::plus)
// Keep track of the remaining height to allocate
var remainingHeight = constraints.maxHeight
// Process the measurables in reverse order, as we will take away space from later measurables first.
val placeables = measurables
.withIndex()
.reversed()
.map { (index, measurable) ->
// Determine the single-line height
val singleLineHeight = measurable.minIntrinsicHeight(Int.MAX_VALUE)
// Retrieve the amount of space needed to fit everything up-to and including this measurable
// while fulfilling the minimum intrinsic size
val heightNeededForMinimumIntrinsicSizesHere =
heightsNeededForMinimumIntrinsicSizes[index]
// Determine the maximum height to measure with
val maxHeight = if (remainingHeight >= heightNeededForMinimumIntrinsicSizesHere) {
// If we have enough space to fit everything completely, just measure with the intrinsic
// size
minimumIntrinsicSizes[index]
} else {
// Retrieve the amount of space needed to fit everything up-to but not including this
// measurable while fulfilling the minimum intrinsic size
val heightNeededForMinimumIntrinsicSizesBefore =
heightsNeededForMinimumIntrinsicSizes.getOrElse(index - 1) { 0 }
if (remainingHeight >= heightNeededForMinimumIntrinsicSizesBefore + singleLineHeight) {
// If we have enough space to fit everything before this measurable, measure this one
// with the excess space
remainingHeight - heightNeededForMinimumIntrinsicSizesBefore
} else {
// Otherwise, only allocate this measurable the single-line height
singleLineHeight
}
}
val placeable = measurable.measure(constraints.copy(maxHeight = maxHeight))
// Remove the allocated height
remainingHeight -= placeable.height
placeable
}
.reversed() // Return to top-to-bottom order
layout(placeables.maxOf(Placeable::width), placeables.sumOf(Placeable::height)) {
var y = 0
placeables.forEach { placeable ->
placeable.placeRelative(0, y)
y += placeable.height
}
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment