Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ fun HtmlText(
URLSpanStyle: SpanStyle = SpanStyle(
color = linkTextColor(),
textDecoration = TextDecoration.Underline
)
),
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null,
useCustomSpannedHandler: Boolean = customSpannedHandler != null
)
```

Expand Down
22 changes: 14 additions & 8 deletions htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ireward.htmlcompose

import android.text.Spanned
import android.text.style.*
import android.widget.TextView
import androidx.compose.foundation.text.ClickableText
Expand All @@ -8,10 +9,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.*
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
Expand All @@ -35,9 +33,11 @@ fun HtmlText(
URLSpanStyle: SpanStyle = SpanStyle(
color = linkTextColor(),
textDecoration = TextDecoration.Underline
)
),
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null,
useCustomSpannedHandler: Boolean = customSpannedHandler != null
) {
val content = text.asHTML(fontSize, flags, URLSpanStyle)
val content = text.asHTML(fontSize, flags, URLSpanStyle, customSpannedHandler, useCustomSpannedHandler)
if (linkClicked != null) {
ClickableText(
modifier = modifier,
Expand Down Expand Up @@ -77,12 +77,18 @@ private fun linkTextColor() = Color(
private fun String.asHTML(
fontSize: TextUnit,
flags: Int,
URLSpanStyle: SpanStyle
URLSpanStyle: SpanStyle,
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null,
useCustomSpannedHandler: Boolean = customSpannedHandler != null
) = buildAnnotatedString {
val spanned = HtmlCompat.fromHtml(this@asHTML, flags)
val spans = spanned.getSpans(0, spanned.length, Any::class.java)

append(spanned.toString())
if (useCustomSpannedHandler) {
append(customSpannedHandler!!(spanned))
} else {
append(spanned.toString())
}

spans
.filter { it !is BulletSpan }
Expand Down