Skip to content

Commit

Permalink
[add][#40] Truncate text on overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed Nov 29, 2023
1 parent b7736f3 commit ff47ec1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.jeziellago.compose.markdowntext
import android.content.Context
import android.graphics.Paint
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.TypedValue
Expand All @@ -26,6 +28,7 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.doOnNextLayout
import androidx.core.widget.TextViewCompat
import coil.ImageLoader
import io.noties.markwon.AbstractMarkwonPlugin
Expand Down Expand Up @@ -56,6 +59,7 @@ fun MarkdownText(
linkColor: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
textAlign: TextAlign? = null,
truncateOnTextOverflow: Boolean = false,
lineHeight: TextUnit = TextUnit.Unspecified,
maxLines: Int = Int.MAX_VALUE,
isTextSelectable: Boolean = false,
Expand Down Expand Up @@ -91,6 +95,7 @@ fun MarkdownText(
autoSizeConfig = autoSizeConfig,
style = style,
textAlign = textAlign,
truncateOnTextOverflow = truncateOnTextOverflow,
lineHeight = lineHeight,
viewId = viewId,
onClick = onClick,
Expand Down Expand Up @@ -118,6 +123,7 @@ private fun createTextView(
defaultColor: Color,
fontSize: TextUnit = TextUnit.Unspecified,
textAlign: TextAlign? = null,
truncateOnTextOverflow: Boolean = false,
lineHeight: TextUnit,
maxLines: Int = Int.MAX_VALUE,
isTextSelectable: Boolean = false,
Expand Down Expand Up @@ -181,6 +187,22 @@ private fun createTextView(
typeface = ResourcesCompat.getFont(context, font)
}

if (truncateOnTextOverflow) {
doOnNextLayout {
if (maxLines != -1 && lineCount > maxLines) {
val endOfLastLine = layout.getLineEnd(maxLines - 1)
val spannedDropLast3Chars = text.subSequence(0, endOfLastLine - 3) as? Spanned
if (spannedDropLast3Chars != null) {
val spannableBuilder = SpannableStringBuilder()
.append(spannedDropLast3Chars)
.append("")

text = spannableBuilder
}
}
}
}

autoSizeConfig?.let { config ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setAutoSizeTextTypeUniformWithConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Card
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import dev.jeziellago.compose.markdowntext.MarkdownText

Expand Down Expand Up @@ -47,6 +48,7 @@ class MainActivity : AppCompatActivity() {
markdown = rawMarkdown,
viewId =
R.id.markdownTextId,
textAlign = TextAlign.Justify,
disableLinkMovementMethod = false
)
}
Expand Down
4 changes: 4 additions & 0 deletions sample/src/main/res/raw/markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Justified 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.

---
__Advertisement :)__

Expand Down

0 comments on commit ff47ec1

Please sign in to comment.