From f60b630b51605f14a4e19f25781702046cfc61df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ygor=20Fraz=C3=A3o?= Date: Mon, 27 Mar 2023 10:42:59 -0300 Subject: [PATCH 1/2] Create README.md --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..77e4dc5 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# highlighted-text-compose +Convenience composable functions to highlight bits of texts + + +
+ + LinkedIn Badge + + + Youtube Badge + +
+ +## How can i use it? + +Just add this to your *settings.gradle*: + +```groovy +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + maven { url 'https://jitpack.io' } + } +} +``` + +Then, in your *build.gradle*: + +```groovy + +``` + +## What it does? + +Have you ever used `buildAnnotatedString` function? if yes, you know you will have a lot of rework for each spannable you want to build. This lib is a convenience to cover most use cases. For me, it was created to highlight sentences in a random text, like: + From 234549e0aae52ae864e17d670f1b555527309171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ygor=20Fraz=C3=A3o?= Date: Mon, 27 Mar 2023 11:00:05 -0300 Subject: [PATCH 2/2] Update README.md --- README.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/README.md b/README.md index 77e4dc5..2cb6a40 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,95 @@ Then, in your *build.gradle*: Have you ever used `buildAnnotatedString` function? if yes, you know you will have a lot of rework for each spannable you want to build. This lib is a convenience to cover most use cases. For me, it was created to highlight sentences in a random text, like: +![image](https://user-images.githubusercontent.com/17025709/227956521-5647fcf4-e33c-4d3a-9d5c-cf2c3512f1d8.png)![image](https://user-images.githubusercontent.com/17025709/227956782-deca0765-4cc2-4e84-bc49-477a89ee57f1.png) + +There are two functions that you can use to achieve that result: + +```kotlin +@Composable +fun HighlightedText( + text: String, + highlightedSentences: List, + normalTextSpanStyle: SpanStyle, + highlightedSentencesTextSpanStyle: SpanStyle, + ignoreCase: Boolean = true, + content: (@Composable (AnnotatedString) -> Unit) +) +``` +Which will pass the result in a lambda Composable block. + +And + +```kotlin +@Composable +fun highlightedText( + text: String, + highlightedSentences: List, + normalTextSpanStyle: SpanStyle, + highlightedSentencesTextSpanStyle: SpanStyle, + ignoreCase: Boolean = true +): AnnotatedString +``` + +Which will return an AnnotatedString. + +In the above example, something like this was used: + +```kotlin +HighlightedText( + text = note.text, + highlightedSentences = highlightSentences, + normalTextSpanStyle = MaterialTheme.typography.bodyMedium.toSpanStyle(), + highlightedSentencesTextSpanStyle = MaterialTheme.typography.bodyMedium.copy( + color = LocalTextSelectionColors.current.handleColor, + background = LocalTextSelectionColors.current.backgroundColor + ).toSpanStyle() +) { + Text( + modifier = Modifier + .padding(top = MaterialTheme.spacing.small), + textAlign = TextAlign.Justify, + text = it, + style = MaterialTheme.typography.bodyMedium + ) +} +``` + +## Clickable Text +It is included a wraper to material3 ClickableText as well, as the submodule depedency: +```groovy + +``` +Can produce results like this: + +![image](https://user-images.githubusercontent.com/17025709/227959923-2c95ff6a-0d02-496a-9720-7373385496a0.png) + + +with the signature: +```kotlin +@Composable +fun ReusableClickableText( + modifier: Modifier = Modifier, + text: String, + clickableParts: Map Unit>, + normalTextSpanStyle: SpanStyle, + clickableTextSpanStyle: SpanStyle = normalTextSpanStyle.copy(color = Color.Blue) +) +``` + +In the above case, the function is used like this: + +```kotlin +ReusableClickableText( + modifier = Modifier.padding(top = MaterialTheme.spacing.small), + text = "Click here to clear search query", + clickableParts = mapOf("here" to { viewModel.clearFilter() }), + normalTextSpanStyle = LocalTextStyle.current.toSpanStyle() + .copy(color = LocalContentColor.current) +) +``` + +And, that's all, hope it can help you. + +## Other notes +You can do whatever you see fancy with the code.