Created
February 23, 2022 21:35
-
-
Save DmytroShuba/40749a29f6919f396c219925ab42cc31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun attachLink( | |
source: String, | |
segment: String, | |
link: String | |
): AnnotatedString { | |
val builder = AnnotatedString.Builder() // builder to attach metadata(link) | |
builder.append(source) // load current text into the builder | |
val start = source.indexOf(segment) // get the start of the span "my website" | |
val end = start + segment.length // get the end of the span | |
val hyperlinkStyle = SpanStyle( | |
color = Color.Blue, | |
textDecoration = TextDecoration.Underline | |
) // create a hyperlink text style | |
builder.addStyle(hyperlinkStyle, start, end) // style "my website" to make it look like a link | |
builder.addStringAnnotation(TAG_URL, link, start, end) // attach the link to the span. We can then access it via the TAG_URL | |
return builder.toAnnotatedString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment