Created
November 17, 2025 08:50
-
-
Save psinetron/febc1e9ed2e716d94a6e024dd4bfbe99 to your computer and use it in GitHub Desktop.
TextWithCustomEmoji
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
| @Composable | |
| fun TextWithCustomEmoji( | |
| text: String, | |
| modifier: Modifier = Modifier, | |
| emojis: List<Emoji> = EMOJIS | |
| ) { | |
| val emojiRanges = emojis.findEmojisInText(text) | |
| val annotatedString = buildAnnotatedString { | |
| var lastIndex = 0 | |
| emojiRanges.forEach { emojiRange -> | |
| append(text.substring(lastIndex, emojiRange.range.first)) | |
| appendInlineContent( | |
| id = "emoji_${emojiRange.range.first}", | |
| alternateText = emojiRange.emoji.emoji | |
| ) | |
| lastIndex = emojiRange.range.last + 1 | |
| } | |
| if (lastIndex < text.length) { | |
| append(text.substring(lastIndex)) | |
| } | |
| } | |
| val inlineContent = emojiRanges.associate { emojiRange -> | |
| "emoji_${emojiRange.range.first}" to InlineTextContent( | |
| placeholder = Placeholder( | |
| width = 20.sp, | |
| height = 20.sp, | |
| placeholderVerticalAlign = PlaceholderVerticalAlign.TextCenter | |
| ) | |
| ) { | |
| Image( | |
| painter = painterResource(id = emojiRange.emoji.resource), | |
| contentDescription = emojiRange.emoji.emoji | |
| ) | |
| } | |
| } | |
| Text( | |
| text = annotatedString, | |
| inlineContent = inlineContent, | |
| modifier = modifier | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment