Created
September 2, 2021 06:19
-
-
Save shmehdi01/d1a1d60becc6397a763a6f139af112d8 to your computer and use it in GitHub Desktop.
Create Spannable and LikifyText in Compose
This file contains hidden or 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
package shmehdi.jet.components | |
import androidx.compose.foundation.text.ClickableText | |
import androidx.compose.foundation.text.InlineTextContent | |
import androidx.compose.material.LocalTextStyle | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.text.* | |
import androidx.compose.ui.text.font.FontFamily | |
import androidx.compose.ui.text.font.FontStyle | |
import androidx.compose.ui.text.font.FontWeight | |
import androidx.compose.ui.text.style.TextAlign | |
import androidx.compose.ui.text.style.TextDecoration | |
import androidx.compose.ui.text.style.TextOverflow | |
import androidx.compose.ui.unit.TextUnit | |
@Composable | |
fun SpannableText( | |
text: String, | |
spanText: String, | |
modifier: Modifier = Modifier, | |
spanStyle: SpanStyle = SpanStyle(color = Color.Blue), | |
color: Color = Color.Unspecified, | |
fontSize: TextUnit = TextUnit.Unspecified, | |
fontStyle: FontStyle? = null, | |
fontWeight: FontWeight? = null, | |
fontFamily: FontFamily? = null, | |
letterSpacing: TextUnit = TextUnit.Unspecified, | |
textDecoration: TextDecoration? = null, | |
textAlign: TextAlign? = null, | |
lineHeight: TextUnit = TextUnit.Unspecified, | |
overflow: TextOverflow = TextOverflow.Clip, | |
softWrap: Boolean = true, | |
maxLines: Int = Int.MAX_VALUE, | |
inlineContent: Map<String, InlineTextContent> = mapOf(), | |
onTextLayout: (TextLayoutResult) -> Unit = {}, | |
style: TextStyle = LocalTextStyle.current, | |
) { | |
val annotatedString = buildSpannable(text, spanText,spanStyle, false) | |
Text( | |
text = annotatedString, | |
modifier, | |
color, | |
fontSize, | |
fontStyle, | |
fontWeight, | |
fontFamily, | |
letterSpacing, | |
textDecoration, | |
textAlign, | |
lineHeight, | |
overflow, | |
softWrap, | |
maxLines, | |
inlineContent, | |
onTextLayout, | |
style | |
) | |
} | |
@Composable | |
fun SpannableText( | |
text: String, | |
spanText: String, | |
modifier: Modifier = Modifier, | |
spanStyle: SpanStyle = SpanStyle(color = Color.Blue), | |
style: TextStyle = LocalTextStyle.current, | |
overflow: TextOverflow = TextOverflow.Clip, | |
softWrap: Boolean = true, | |
maxLines: Int = Int.MAX_VALUE, | |
onTextLayout: (TextLayoutResult) -> Unit = {}, | |
onClick: (String) -> Unit, | |
) { | |
val annotatedString = buildSpannable(text, spanText,spanStyle, true) | |
ClickableText( | |
text = annotatedString, | |
modifier, | |
style, | |
softWrap, | |
overflow, | |
maxLines, | |
onTextLayout, | |
onClick = { offset -> | |
spanText.split(" ").forEach { tag -> | |
annotatedString.getStringAnnotations(tag, offset, offset).firstOrNull()?.let { | |
onClick.invoke(it.item) | |
} | |
} | |
} | |
) | |
} | |
private fun buildSpannable( | |
text: String, | |
spanText: String, | |
spanStyle: SpanStyle, | |
isClickable: Boolean | |
) = buildAnnotatedString { | |
text.split(" ").forEach { | |
if (spanText.contains(it)) { | |
if (isClickable) pushStringAnnotation(it, it) | |
withStyle(style = spanStyle) { | |
append("$it ") | |
} | |
if (isClickable) pop() | |
} else { | |
append("$it ") | |
} | |
} | |
} |
Author
shmehdi01
commented
Sep 2, 2021
This code will not work if there are two annotated strings.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment