Skip to content

Commit 1da22a0

Browse files
authored
Add basic HTML text styling example (#389)
* Add basic HTML text styling example * Apply Spotless * Add region tags --------- Co-authored-by: jakeroseman <[email protected]>
1 parent 81c1a2a commit 1da22a0

File tree

1 file changed

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

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.text
18+
19+
import androidx.compose.material3.Text
20+
import androidx.compose.runtime.Composable
21+
import androidx.compose.ui.Modifier
22+
import androidx.compose.ui.graphics.Color
23+
import androidx.compose.ui.text.AnnotatedString
24+
import androidx.compose.ui.text.SpanStyle
25+
import androidx.compose.ui.text.TextLinkStyles
26+
import androidx.compose.ui.text.font.FontStyle
27+
import androidx.compose.ui.text.fromHtml
28+
import androidx.compose.ui.text.style.TextDecoration
29+
import androidx.compose.ui.tooling.preview.Preview
30+
31+
// [START android_compose_text_annotatedhtmlstringwithlink]
32+
@Composable
33+
fun AnnotatedHtmlStringWithLink(
34+
modifier: Modifier = Modifier,
35+
htmlText: String = """
36+
<h1>Jetpack Compose</h1>
37+
<p>
38+
Build <b>better apps</b> faster with <a href="https://www.android.com">Jetpack Compose</a>
39+
</p>
40+
""".trimIndent()
41+
) {
42+
Text(
43+
AnnotatedString.fromHtml(
44+
htmlText,
45+
linkStyles = TextLinkStyles(
46+
style = SpanStyle(
47+
textDecoration = TextDecoration.Underline,
48+
fontStyle = FontStyle.Italic,
49+
color = Color.Blue
50+
)
51+
)
52+
),
53+
modifier
54+
)
55+
}
56+
// [END android_compose_text_annotatedhtmlstringwithlink]
57+
58+
@Preview(showBackground = true)
59+
@Composable
60+
private fun AnnotatedHtmlStringWithLinkPreview() {
61+
AnnotatedHtmlStringWithLink()
62+
}

0 commit comments

Comments
 (0)