11package com.ireward.htmlcompose
22
3+ import android.text.Spanned
34import android.text.style.*
45import android.widget.TextView
56import androidx.compose.foundation.text.ClickableText
@@ -8,10 +9,7 @@ import androidx.compose.runtime.Composable
89import androidx.compose.ui.Modifier
910import androidx.compose.ui.graphics.Color
1011import androidx.compose.ui.platform.LocalContext
11- import androidx.compose.ui.text.SpanStyle
12- import androidx.compose.ui.text.TextLayoutResult
13- import androidx.compose.ui.text.TextStyle
14- import androidx.compose.ui.text.buildAnnotatedString
12+ import androidx.compose.ui.text.*
1513import androidx.compose.ui.text.style.TextDecoration
1614import androidx.compose.ui.text.style.TextOverflow
1715import androidx.compose.ui.unit.TextUnit
@@ -35,9 +33,10 @@ fun HtmlText(
3533 URLSpanStyle : SpanStyle = SpanStyle (
3634 color = linkTextColor(),
3735 textDecoration = TextDecoration .Underline
38- )
36+ ),
37+ customSpannedHandler : ((Spanned ) -> AnnotatedString )? = null
3938) {
40- val content = text.asHTML(fontSize, flags, URLSpanStyle )
39+ val content = text.asHTML(fontSize, flags, URLSpanStyle , customSpannedHandler )
4140 if (linkClicked != null ) {
4241 ClickableText (
4342 modifier = modifier,
@@ -77,12 +76,17 @@ private fun linkTextColor() = Color(
7776private fun String.asHTML (
7877 fontSize : TextUnit ,
7978 flags : Int ,
80- URLSpanStyle : SpanStyle
79+ URLSpanStyle : SpanStyle ,
80+ customSpannedHandler : ((Spanned ) -> AnnotatedString )? = null
8181) = buildAnnotatedString {
8282 val spanned = HtmlCompat .fromHtml(this @asHTML, flags)
8383 val spans = spanned.getSpans(0 , spanned.length, Any ::class .java)
8484
85- append(spanned.toString())
85+ if (customSpannedHandler != null ) {
86+ append(customSpannedHandler(spanned))
87+ } else {
88+ append(spanned.toString())
89+ }
8690
8791 spans
8892 .filter { it !is BulletSpan }
@@ -114,4 +118,4 @@ private fun String.asHTML(
114118 addStyle(spanStyle, start, end)
115119 }
116120 }
117- }
121+ }
0 commit comments