Skip to content
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add to app module *gradle.build* file.

```gradle
dependencies {
implementation 'com.github.ireward.compose-html:1.0.1'
implementation 'com.github.viluahealthcare.compose-html:1.0.0'
}
```

Expand Down Expand Up @@ -54,7 +54,8 @@ fun HtmlText(
URLSpanStyle: SpanStyle = SpanStyle(
color = linkTextColor(),
textDecoration = TextDecoration.Underline
)
),
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null
)
```

Expand Down
2 changes: 1 addition & 1 deletion htmlcompose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'kotlin-android'
id 'maven-publish'
}
group = 'com.github.ireward'
group = 'com.github.viluahealthcare'
version = '1.0.0'

android {
Expand Down
22 changes: 13 additions & 9 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,10 @@ fun HtmlText(
URLSpanStyle: SpanStyle = SpanStyle(
color = linkTextColor(),
textDecoration = TextDecoration.Underline
)
),
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null
) {
val content = text.asHTML(fontSize, flags, URLSpanStyle)
val content = text.asHTML(fontSize, flags, URLSpanStyle, customSpannedHandler)
if (linkClicked != null) {
ClickableText(
modifier = modifier,
Expand Down Expand Up @@ -77,12 +76,17 @@ private fun linkTextColor() = Color(
private fun String.asHTML(
fontSize: TextUnit,
flags: Int,
URLSpanStyle: SpanStyle
URLSpanStyle: SpanStyle,
customSpannedHandler: ((Spanned) -> AnnotatedString)? = null
) = buildAnnotatedString {
val spanned = HtmlCompat.fromHtml(this@asHTML, flags)
val spans = spanned.getSpans(0, spanned.length, Any::class.java)

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

spans
.filter { it !is BulletSpan }
Expand Down Expand Up @@ -114,4 +118,4 @@ private fun String.asHTML(
addStyle(spanStyle, start, end)
}
}
}
}