Add UiUtils method to render Spannables (from HtmlCompat) with Compose (bitfireAT/davx5#430)

* Add Spanned.toAnnotatedString, minor changes

---------

Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
Sunik Kupfer 2023-11-01 11:51:20 +01:00 committed by Ricki Hirner
parent ca26155eed
commit fbe0c4451b
No known key found for this signature in database
GPG key ID: 79A019FCAAEDD3AA
2 changed files with 34 additions and 1 deletions

View file

@ -9,13 +9,22 @@ import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Typeface
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.text.Spanned
import android.text.style.StyleSpan
import android.widget.Toast
import androidx.appcompat.app.AppCompatDelegate
import androidx.browser.customtabs.CustomTabsClient
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.core.content.getSystemService
import androidx.core.text.getSpans
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.Settings
@ -88,4 +97,28 @@ object UiUtils {
}
}
@Composable
fun Spanned.toAnnotatedString() = buildAnnotatedString {
val spanned = this@toAnnotatedString
append(spanned.toString())
for (span in getSpans<Any>(0, spanned.length)) {
val start = getSpanStart(span)
val end = getSpanEnd(span)
when (span) {
is StyleSpan ->
when (span.style) {
Typeface.BOLD -> addStyle(
SpanStyle(fontWeight = FontWeight.Bold),
start = start, end = end
)
Typeface.ITALIC -> addStyle(
SpanStyle(fontStyle = FontStyle.Italic),
start = start, end = end
)
}
}
}
}
}

View file

@ -6,7 +6,7 @@ buildscript {
ext.versions = [
aboutLibraries: '10.9.1',
appIntro: '7.0.0-beta02',
composeBom: '2023.10.00',
composeBom: '2023.10.01',
hilt: '2.48.1',
kotlin: '1.9.10', // keep in sync with * app/build.gradle composeOptions.kotlinCompilerExtensionVersion
// * com.google.devtools.ksp at the end of this file