Skip to content
This repository was archived by the owner on Mar 12, 2026. It is now read-only.

Commit 9f1a6bc

Browse files
Merge pull request #1 from viluahealthcare/va/span-handling
Add Option For Custom Span Handling
2 parents f43b39a + aedf801 commit 9f1a6bc

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Add to app module *gradle.build* file.
1919

2020
```gradle
2121
dependencies {
22-
implementation 'com.github.ireward.compose-html:1.0.1'
22+
implementation 'com.github.viluahealthcare.compose-html:1.0.0'
2323
}
2424
```
2525

@@ -54,7 +54,8 @@ fun HtmlText(
5454
URLSpanStyle: SpanStyle = SpanStyle(
5555
color = linkTextColor(),
5656
textDecoration = TextDecoration.Underline
57-
)
57+
),
58+
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null
5859
)
5960
```
6061

htmlcompose/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id 'kotlin-android'
44
id 'maven-publish'
55
}
6-
group = 'com.github.ireward'
6+
group = 'com.github.viluahealthcare'
77
version = '1.0.0'
88

99
android {

htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ireward.htmlcompose
22

3+
import android.text.Spanned
34
import android.text.style.*
45
import android.widget.TextView
56
import androidx.compose.foundation.text.ClickableText
@@ -8,10 +9,7 @@ import androidx.compose.runtime.Composable
89
import androidx.compose.ui.Modifier
910
import androidx.compose.ui.graphics.Color
1011
import 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.*
1513
import androidx.compose.ui.text.style.TextDecoration
1614
import androidx.compose.ui.text.style.TextOverflow
1715
import 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(
7776
private 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

Comments
 (0)