Skip to content

Commit

Permalink
Add ForegroundColorSpan interpretation (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
juost authored Jun 20, 2022
1 parent b857d93 commit c897282
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +21,7 @@ class MainActivity : ComponentActivity() {
Column(Modifier.background(color = MaterialTheme.colors.background)) {
Greeting()
StringGreeting()
ColorText()
}
}
}
Expand All @@ -39,6 +38,11 @@ fun StringGreeting(){
HtmlText(text = "Hello <b>World</b>. This <i><strike>text</strike>sentence</i> is form<b>att<u>ed</u></b> in simple html. <a href=\"https://github.com/ch4rl3x/HtmlText\">HtmlText</a>")
}

@Composable
fun ColorText() {
HtmlText(text = "Hello <span style=\"color: #00FFFF\">blue</span> world")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
Expand Down
13 changes: 10 additions & 3 deletions html-text/src/main/java/de/charlex/compose/HtmlText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<URLSpan>()
val styleSpans = getSpans<StyleSpan>()
val colorSpans = getSpans<ForegroundColorSpan>()
val underlineSpans = getSpans<UnderlineSpan>()
val strikethroughSpans = getSpans<StrikethroughSpan>()
urlSpans.forEach { urlSpan ->
Expand All @@ -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)
Expand Down

0 comments on commit c897282

Please sign in to comment.