Zoff
02/25/2024, 10:44 AMDima Avdeev
02/25/2024, 2:43 PMimport androidx.compose.foundation.text.ClickableText
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import java.awt.Desktop
import java.net.URI
fun main() = application {
Window(onCloseRequest = ::exitApplication, title = "SampleForAllReproducers") {
val urlTag = "URL"
val urlToNavigate = "<https://github.com/JetBrains/compose-multiplatform>"
val annotatedText = buildAnnotatedString {
append("Click to link: ")
withStyle(SpanStyle(color = Color.Blue, fontWeight = FontWeight.Bold)) {
val linkText = "Compose Multiplatform"
append(linkText)
addStringAnnotation(
tag = urlTag,
annotation = urlToNavigate,
start = length - linkText.length,
end = length
)
}
append(" and some text after")
}
ClickableText(
text = annotatedText,
onClick = { offset ->
val isClickedToUrl = annotatedText.getStringAnnotations(
start = offset,
end = offset
).any { it.tag == urlTag }
if (isClickedToUrl) {
Desktop.getDesktop().browse(URI(urlToNavigate))
}
}
)
}
}
Zoff
02/25/2024, 5:50 PMZach Klippenstein (he/him) [MOD]
02/26/2024, 6:08 PMrob42
02/26/2024, 9:48 PMLinkableText(
text = "Here a [lovely website](<https://www.oracle.com/>) for you",
style = TextStyle.Default,
) {
// "it" will contain "<https://www.oracle.com/>", do something with it.
}
Zoff
02/27/2024, 3:10 PMMarcin Wisniowski
02/29/2024, 1:51 PM