fix: Compose 显示表情

This commit is contained in:
HuanCheng65 2024-01-29 16:19:13 +08:00
parent 392e314618
commit 2cf07d8e62
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
2 changed files with 31 additions and 2 deletions

View File

@ -152,7 +152,7 @@ fun EmoticonText(
val emoticonInlineContent =
remember(sizePx) { EmoticonManager.getEmoticonInlineContent(sizePx * emoticonSize) }
IconText(
text,
text.emoticonString,
modifier,
color,
fontSize,
@ -343,7 +343,6 @@ fun TextWithMinWidth(
)
}
@OptIn(ExperimentalTextApi::class)
@Composable
fun buildChipInlineContent(
text: String,

View File

@ -5,7 +5,9 @@ import android.widget.AdapterView
import android.widget.EditText
import androidx.annotation.DrawableRes
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withAnnotation
import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.adapters.EmoticonGridViewAdapter
import com.huanchengfly.tieba.post.utils.StringUtil.getEmoticonContent
@ -119,10 +121,38 @@ object EmoticonUtil {
}
}
@OptIn(ExperimentalTextApi::class)
val String.emoticonString: AnnotatedString
get() {
val regexPattern = Pattern.compile(getRegex(EMOTICON_ALL_TYPE))
val matcher = regexPattern.matcher(this)
return buildAnnotatedString {
withAnnotation("Emoticon", "true") {
append(this@emoticonString)
}
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
val emoticonName = matcher.group(1)
if (emoticonName != null) {
addStringAnnotation(
INLINE_CONTENT_TAG,
"Emoticon#${EmoticonManager.getEmoticonIdByName(emoticonName)}",
start,
end,
)
}
}
}
}
val AnnotatedString.emoticonString: AnnotatedString
get() {
if (hasStringAnnotations("Emoticon", 0, length)) {
return this
}
val regexPattern = Pattern.compile(getRegex(EMOTICON_ALL_TYPE))
val matcher = regexPattern.matcher(this.text)
return buildAnnotatedString {
append(this@emoticonString)
while (matcher.find()) {