Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix runtime crash on BasicReadMoreText #66

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import kotlin.math.max

/**
* Basic element that displays text with read more.
Expand Down Expand Up @@ -436,9 +437,18 @@ private class ReadMoreState(
textLayout.isLineEllipsized(lastLineIndex)
) {
val countUntilMaxLine = textLayout.getLineEnd(readMoreMaxLines - 1, visibleEnd = true)
val countUntilMaxLineExceptNewline: Int =
if (originalText.getOrNull(countUntilMaxLine) == '\n') {
// Workaround:
// If the last character of the sentence is a newline char('\n'),
// calculates excluding the last newline char('\n').
countUntilMaxLine - 1
} else {
countUntilMaxLine
}
val readMoreWidth = overflowTextLayout.size.width + readMoreTextLayout.size.width
val maximumWidth = textLayout.size.width - readMoreWidth
var replacedEndIndex = countUntilMaxLine + 1
val maximumWidth = max(0, textLayout.layoutInput.constraints.maxWidth - readMoreWidth)
var replacedEndIndex = countUntilMaxLineExceptNewline + 1
var currentTextBounds: Rect
do {
replacedEndIndex -= 1
Expand Down
Loading