From c897282f240639b85fbddd4487aeab9883ee067d Mon Sep 17 00:00:00 2001
From: juost <33381665+juost@users.noreply.github.com>
Date: Mon, 20 Jun 2022 21:43:03 +0200
Subject: [PATCH] Add ForegroundColorSpan interpretation (#4)
---
.../compose/htmltext/example/MainActivity.kt | 8 ++++++--
.../src/main/java/de/charlex/compose/HtmlText.kt | 13 ++++++++++---
2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/example/src/main/java/de/charlex/compose/htmltext/example/MainActivity.kt b/example/src/main/java/de/charlex/compose/htmltext/example/MainActivity.kt
index a9b7d62..0a5c0fd 100644
--- a/example/src/main/java/de/charlex/compose/htmltext/example/MainActivity.kt
+++ b/example/src/main/java/de/charlex/compose/htmltext/example/MainActivity.kt
@@ -6,8 +6,6 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.material.MaterialTheme
-import androidx.compose.material.Surface
-import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
@@ -23,6 +21,7 @@ class MainActivity : ComponentActivity() {
Column(Modifier.background(color = MaterialTheme.colors.background)) {
Greeting()
StringGreeting()
+ ColorText()
}
}
}
@@ -39,6 +38,11 @@ fun StringGreeting(){
HtmlText(text = "Hello World. This textsentence is formatted in simple html. HtmlText")
}
+@Composable
+fun ColorText() {
+ HtmlText(text = "Hello blue world")
+}
+
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
diff --git a/html-text/src/main/java/de/charlex/compose/HtmlText.kt b/html-text/src/main/java/de/charlex/compose/HtmlText.kt
index 762da01..5eadbb1 100644
--- a/html-text/src/main/java/de/charlex/compose/HtmlText.kt
+++ b/html-text/src/main/java/de/charlex/compose/HtmlText.kt
@@ -8,6 +8,7 @@ import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
+import android.text.style.ForegroundColorSpan
import androidx.annotation.StringRes
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.text.InlineTextContent
@@ -218,14 +219,15 @@ fun CharSequence.toAnnotatedString(
fun Spanned.toAnnotatedString(
urlSpanStyle: SpanStyle = SpanStyle(
-color = Color.Blue,
-textDecoration = TextDecoration.Underline
-)
+ color = Color.Blue,
+ textDecoration = TextDecoration.Underline
+ )
): AnnotatedString {
return buildAnnotatedString {
append(this@toAnnotatedString.toString())
val urlSpans = getSpans()
val styleSpans = getSpans()
+ val colorSpans = getSpans()
val underlineSpans = getSpans()
val strikethroughSpans = getSpans()
urlSpans.forEach { urlSpan ->
@@ -234,6 +236,11 @@ textDecoration = TextDecoration.Underline
addStyle(urlSpanStyle, start, end)
addStringAnnotation("url", urlSpan.url, start, end) // NON-NLS
}
+ colorSpans.forEach { colorSpan ->
+ val start = getSpanStart(colorSpan)
+ val end = getSpanEnd(colorSpan)
+ addStyle(SpanStyle(color = Color(colorSpan.foregroundColor)), start, end)
+ }
styleSpans.forEach { styleSpan ->
val start = getSpanStart(styleSpan)
val end = getSpanEnd(styleSpan)