KMP AnnotatedString Creating Multi Style Clickable Text with Link

The AnnotatedString text container widget differs from the standard Text widget in Compose Multiplatform. It aims to create rich, multiple-style text within a single Text widget. In today’s tutorial, we will integrate multiple-style text and clickable text with a custom URL link and apply different text styles on the part of the text.

KMP AnnotatedString Creating Multi Style Clickable Text with Link

KMP AnnotatedString Creating Multi Style Clickable Text with Link

Start Coding for the app:

Code explanation:

  1. LocalUriHandler.current: It retrieves a URI handler to open your custom URL in the device’s default browser application.
  2. buildAnnotatedString { append(“Your Text”)}: Create a AnnotatedString widget. The append property adds part of the text to our custom string one after another.
  3. pushStringAnnotation: The tag property defines this string as a URL string, and then the annotation will be used to assign a URL.
  4. withStyle: To apply a custom Style on that particular part of the text.
  5. pop(): End the current context. So, the style applied to this section will not be applied to incoming append text.

Complete source code for App.kt file:

Screenshot in Android:

clickable text with a custom URL link

Screenshot in iOS:

buildAnnotatedString

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *