Skip to content

Links

The Rich Text Editor provides comprehensive support for hyperlinks, allowing you to: - Add links to new or existing text - Update link URLs - Remove links - Customize link appearance - Handle link clicks

To add a new text with a link, use the addLink method:

// Add link after selection
richTextState.addLink(
    text = "Compose Rich Editor",
    url = "https://github.com/MohamedRejeb/Compose-Rich-Editor"
)

To convert selected text into a link, use the addLinkToSelection method:

// Add link to selected text
richTextState.addLinkToSelection(
    url = "https://kotlinlang.org/"
)

To update an existing link's URL:

// Update selected link URL
richTextState.updateLink(
    url = "https://kotlinlang.org/"
)

To remove a link while keeping the text:

// Remove link from selected text
richTextState.removeLink()

To check if the current selection is a link:

val isLink = richTextState.isLink

To get the current link's text and URL:

// Get link text and URL
val linkText = richTextState.selectedLinkText
val linkUrl = richTextState.selectedLinkUrl

You can customize how links appear in the editor:

richTextState.config.linkColor = Color.Blue
richTextState.config.linkTextDecoration = TextDecoration.Underline

By default, links are opened by your platform's UriHandler. To customize link handling:

val myUriHandler = remember {
    object : UriHandler {
        override fun openUri(uri: String) {
            // Custom link handling logic
            // For example: open in specific browser, validate URL, etc.
        }
    }
}

CompositionLocalProvider(LocalUriHandler provides myUriHandler) {
    RichText(
        state = richTextState,
        modifier = Modifier.fillMaxWidth()
    )
}