compose-html

Additional

Language
Kotlin
Version
N/A
Created
Jan 13, 2022
Updated
Sep 11, 2023
Owner
iRewardHealth (ireward)
Contributor
Víctor Albertos (VictorAlbertos)
1
Activity
Badge
Generate
Download
Source code

HtmlCompose

An Android library which provides HTML support for Jetpack Compose texts.

Setup

Add to top level gradle.build file

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

Add to app module gradle.build file.

dependencies {
    implementation 'com.github.ireward:compose-html:1.0.2'
}

Usage

For the time being, the composable Text layout doesn't provide any HTML support. This library fills that gap by exposing the composable HtmlText layout, which is built on top of the Text layout and the Span/Spannable Android classes. Its API goes as follows:

HtmlText(
    text = htmlString,
    linkClicked = { link ->
        Log.d("linkClicked", link)
    }
)

And these are all the available parameters that allows you to change the default behaviour:

fun HtmlText(
    text: String,
    modifier: Modifier = Modifier,
    style: TextStyle = TextStyle.Default,
    softWrap: Boolean = true,
    overflow: TextOverflow = TextOverflow.Clip,
    maxLines: Int = Int.MAX_VALUE,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    linkClicked: (String) -> Unit = {},
    fontSize: TextUnit = 14.sp,
    flags: Int = HtmlCompat.FROM_HTML_MODE_COMPACT,
    URLSpanStyle: SpanStyle = SpanStyle(
        color = linkTextColor(),
        textDecoration = TextDecoration.Underline
    )
)

HtmlText supports almost as many HTML tags as android.widget.TextView does, with the exception of <img> tag and <ul>, being the latter partially supported, as HtmlText renders properly the elements of the list but it does not add the bullet (•)

What follows are screenshots taken from the companion showcase app hosted in this same repo, where all the HTML tags are rendered side by side to compare HtmlText with android.widget.TextView: