Skip to content

Commit 9a07dd0

Browse files
committed
Add basic HTML text styling example
1 parent d081491 commit 9a07dd0

File tree

1 file changed

+46
-0
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example.compose.snippets.text
2+
3+
import androidx.compose.material3.Text
4+
import androidx.compose.runtime.Composable
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.graphics.Color
7+
import androidx.compose.ui.platform.LocalUriHandler
8+
import androidx.compose.ui.text.AnnotatedString
9+
import androidx.compose.ui.text.SpanStyle
10+
import androidx.compose.ui.text.TextLinkStyles
11+
import androidx.compose.ui.text.font.FontStyle
12+
import androidx.compose.ui.text.fromHtml
13+
import androidx.compose.ui.text.style.TextDecoration
14+
import androidx.compose.ui.tooling.preview.Preview
15+
16+
@Composable
17+
fun AnnotatedHtmlStringWithLink(
18+
modifier: Modifier = Modifier,
19+
htmlText: String = """
20+
<h1>Jetpack Compose</h1>
21+
<p>
22+
Build <b>better apps</b> faster with <a href="https://www.android.com">Jetpack Compose</a>
23+
</p>
24+
""".trimIndent()
25+
) {
26+
Text(
27+
AnnotatedString.fromHtml(
28+
htmlText,
29+
linkStyles = TextLinkStyles(
30+
style = SpanStyle(
31+
textDecoration = TextDecoration.Underline,
32+
fontStyle = FontStyle.Italic,
33+
color = Color.Blue
34+
)
35+
)
36+
),
37+
modifier
38+
)
39+
}
40+
41+
42+
@Preview(showBackground = true)
43+
@Composable
44+
private fun AnnotatedHtmlStringWithLinkPreview() {
45+
AnnotatedHtmlStringWithLink()
46+
}

0 commit comments

Comments
 (0)