Skip to content

Commit

Permalink
Fix runtime crash on BasicReadMoreText (#66)
Browse files Browse the repository at this point in the history
* Fix runtime crash on BasicReadMoreText

* Add workaround code to resolve the issue of 'more' text being hidden.
  • Loading branch information
fornewid authored Nov 15, 2023
1 parent fa34777 commit a4cadc0
Showing 1 changed file with 12 additions and 2 deletions.
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

0 comments on commit a4cadc0

Please sign in to comment.