Skip to content

How to modify the style of selected text ? #565

@hjzf

Description

@hjzf

Hi! Thanks for your work.

Is there a way to modify the style of selected text ?

It might be useful to provide a method called setAnnotatedString.

For example, Ctrl + B makes the selected text bold :

val richTextState = rememberRichTextState()
BasicRichTextEditor(
    state = richTextState,
    modifier = Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 8.dp).onKeyEvent {
        if (it.isCtrlPressed && it.key == Key.B) {
            val annotatedString = richTextState.annotatedString
            val selection = richTextState.selection
            val startIndex = selection.min
            val endIndex = selection.max
            val selectedText = annotatedString.substring(startIndex, endIndex)
            val newAnnotatedString = buildAnnotatedString {
                append(annotatedString.subSequence(startIndex = 0, endIndex = startIndex))
                append(
                    buildAnnotatedString {
                        append(selectedText)
                        addStyle(SpanStyle(fontWeight = FontWeight.Bold), 0, selectedText.length)
                    }
                )
                append(annotatedString.subSequence(startIndex = endIndex, endIndex = annotatedString.length))
            }
            // richTextState.setAnnotatedString(newAnnotatedString)
            true
        } else {
            false
        }
    },
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions