@@ -42,11 +42,14 @@ import androidx.compose.ui.graphics.Brush
42
42
import androidx.compose.ui.graphics.Color
43
43
import androidx.compose.ui.graphics.Color.Companion.Cyan
44
44
import androidx.compose.ui.graphics.Shadow
45
+ import androidx.compose.ui.platform.LocalUriHandler
45
46
import androidx.compose.ui.res.stringResource
46
47
import androidx.compose.ui.text.AnnotatedString
48
+ import androidx.compose.ui.text.LinkAnnotation
47
49
import androidx.compose.ui.text.ParagraphStyle
48
50
import androidx.compose.ui.text.PlatformTextStyle
49
51
import androidx.compose.ui.text.SpanStyle
52
+ import androidx.compose.ui.text.TextLinkStyles
50
53
import androidx.compose.ui.text.TextStyle
51
54
import androidx.compose.ui.text.buildAnnotatedString
52
55
import androidx.compose.ui.text.font.Font
@@ -58,6 +61,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
58
61
import androidx.compose.ui.text.style.LineHeightStyle
59
62
import androidx.compose.ui.text.style.TextAlign
60
63
import androidx.compose.ui.text.style.TextOverflow
64
+ import androidx.compose.ui.text.withLink
61
65
import androidx.compose.ui.text.withStyle
62
66
import androidx.compose.ui.unit.dp
63
67
import androidx.compose.ui.unit.em
@@ -577,6 +581,50 @@ private object TextEffectiveStateManagement2 {
577
581
// [END android_compose_text_state_management]
578
582
}
579
583
584
+ // [START android_compose_text_link_1]
585
+ @Composable
586
+ fun AnnotatedStringWithLinkSample () {
587
+ // Display a link in the text
588
+ Text (
589
+ buildAnnotatedString {
590
+ append(" Build better apps faster with " )
591
+ withLink(
592
+ LinkAnnotation .Url (
593
+ " https://developer.android.com/jetpack/compose" ,
594
+ TextLinkStyles (style = SpanStyle (color = Color .Blue ))
595
+ )
596
+ ) {
597
+ append(" Jetpack Compose" )
598
+ }
599
+ }
600
+ )
601
+ }
602
+ // [END android_compose_text_link_1]
603
+
604
+ // [START android_compose_text_link_2]
605
+ @Composable
606
+ fun AnnotatedStringWithListenerSample () {
607
+ // Display a link in the text and log metrics whenever user clicks on it. In that case we handle
608
+ // the link using openUri method of the LocalUriHandler
609
+ val uriHandler = LocalUriHandler .current
610
+ Text (
611
+ buildAnnotatedString {
612
+ append(" Build better apps faster with " )
613
+ val link =
614
+ LinkAnnotation .Url (
615
+ " https://developer.android.com/jetpack/compose" ,
616
+ TextLinkStyles (SpanStyle (color = Color .Blue ))
617
+ ) {
618
+ val url = (it as LinkAnnotation .Url ).url
619
+ // log some metrics
620
+ uriHandler.openUri(url)
621
+ }
622
+ withLink(link) { append(" Jetpack Compose" ) }
623
+ }
624
+ )
625
+ }
626
+ // [END android_compose_text_link_2]
627
+
580
628
private val firaSansFamily = FontFamily ()
581
629
582
630
val LightBlue = Color (0xFF0066FF )
0 commit comments