Skip to content

Instantly share code, notes, and snippets.

View burkido's full-sized avatar

Burak burkido

View GitHub Profile
val annotations = listOf(
StringAnnotation(
tag = stringResource(id = R.string.terms),
annotation = "https://termify.io/terms-and-conditions-generator",
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
fontSize = MaterialTheme.typography.bodyMedium.fontSize,
fontStyle = MaterialTheme.typography.bodyMedium.fontStyle,
textDecoration = TextDecoration.Underline,
)
/**
* Composable function that creates a text with clickable annotations.
*
* @param text The text that will be displayed. It should contain tags that match the tags in the annotations list.
* @param textStyle The style that will be applied to the text. If not provided, the default style is `MaterialTheme.typography.titleSmall`.
* @param stringsAnnotations A list of `LinkAnnotation` objects. Each `LinkAnnotation` contains a tag, an annotation, and a style. The tag is a substring of the text that will be annotated. The annotation is the actual annotation that will be attached to the tag in the text. The style is the style that will be applied to the text span that matches the tag.
* @param onClick A function that will be called when a text span with an annotation is clicked. The function takes the annotation as a parameter.
*/
@Composable
fun ClickableLocalizedText(
@burkido
burkido / AnnotatedText.kt
Created March 23, 2024 15:03
Usage of clickable annotated text.
val annotatedString = buildAnnotatedString {
append(stringResource(id = R.string.prefix_terms))
pushStringAnnotation(tag = stringResource(id = R.string.terms), annotation = "https://termify.io/terms-and-conditions-generator")
withStyle(
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
)
<resources>
<string name="prefix_terms">By clicking the continue, you agree to our\u0020</string>
<string name="terms">Terms</string>
<string name="privacy_policy">Privacy Policy</string>
<string name="and">\u0020and\u0020</string>
<string name="agreement_text">By clicking the continue, you agree to our Terms and Privacy Policy</string>
</resources>
<resources>
<string name="prefix_terms">Devam ederek\u0020</string>
@burkido
burkido / StringAnnotation.kt
Created March 23, 2024 14:43
This data class represents a link annotation used in text formatting, containing properties for the tag, annotation (URL), and the style applied to the corresponding text span. It is marked as immutable (@immutable).
/**
* Data class representing a link annotation.
*
* @property tag The tag that will be used to identify this annotation in the text.
* @property annotation The actual annotation(url) that will be attached to the tag in the text.
* @property style The style that will be applied to the text span that matches the tag.
*/
@Immutable
data class StringAnnotation(
val tag: String,