From aedf801e9c179752af788b36f1ba99127dfe07f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Albertos?= Date: Tue, 7 Feb 2023 15:36:55 +0100 Subject: [PATCH] add span handling --- README.md | 5 +++-- htmlcompose/build.gradle | 2 +- .../java/com/ireward/htmlcompose/HtmlText.kt | 22 +++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6bd508f..228ed92 100644 --- a/README.md +++ b/README.md @@ -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' } ``` @@ -54,7 +54,8 @@ fun HtmlText( URLSpanStyle: SpanStyle = SpanStyle( color = linkTextColor(), textDecoration = TextDecoration.Underline - ) + ), + customSpannedHandler: ((Spanned) -> AnnotatedString)? = null ) ``` diff --git a/htmlcompose/build.gradle b/htmlcompose/build.gradle index e9b7466..abd6e55 100644 --- a/htmlcompose/build.gradle +++ b/htmlcompose/build.gradle @@ -3,7 +3,7 @@ plugins { id 'kotlin-android' id 'maven-publish' } -group = 'com.github.ireward' +group = 'com.github.viluahealthcare' version = '1.0.0' android { diff --git a/htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt b/htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt index 8538dec..85b7bd6 100644 --- a/htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt +++ b/htmlcompose/src/main/java/com/ireward/htmlcompose/HtmlText.kt @@ -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 @@ -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 @@ -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, @@ -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 } @@ -114,4 +118,4 @@ private fun String.asHTML( addStyle(spanStyle, start, end) } } -} +} \ No newline at end of file