0

我的目标是创建一个粗体文本,当字符串中的某些单词或字符用特殊字符(例如 ++bold++)显示为粗体时,Jetpack ComposeText组件在特定字符串中可用的次数越多。

val boldRegex = Regex("\\*\\*.*\\*\\*")

使用这个正则表达式和下面的代码片段

@Composable
fun CustomText(text: String, modifier: Modifier = Modifier) {


    val boldKeywords: MatchResult? = boldRegex.find(text)

    val boldIndexes = mutableListOf<Pair<Int, Int>>()
    boldKeywords?.let {
        boldIndexes.add(Pair(it.range.first, it.range.last - 2))
    }

    val newText = text.replace("**", "")

    val annotatedString = buildAnnotatedString {
        append(newText)

        // Add bold style to keywords that has to be bold
        boldIndexes.forEach {
            addStyle(
                style = SpanStyle(
                    fontWeight = FontWeight.Bold,
                    color = Color(0xff64B5F6),
                    fontSize = 15.sp

                ),
                start = it.first,
                end = it.second
            )

        }
    }

    Text(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 8.dp, end = 8.dp, top = 12.dp, bottom = 12.dp),
        fontSize = 16.sp,
        text = annotatedString
    )
}

在此处输入图像描述

只有当一个确切的模式存在一次并且替换每个出现的字符也不正确时,我才能得到正确的结果。

CustomText(text = "This is a **bold** text")

CustomText(
    text = "This is a **bold** text  and another **random** value build with *regex expression"
)

CustomText(
    text = "This is NOT a ****bold** text build with *regex expression"
)

CustomText(
    text = "This is NOT a **bold text build with *regex expression"
)
  • 在第一个文本结果中是所希望的
  • 在第二个中,它应该只将粗体随机子字符串设为粗体
  • 在第三个中,它不应该有任何粗体子字符串并且没有替换
  • 在第四个中不应该有粗体子串和替换

我检查了这个正则表达式问题,但我无法提出正确的正则表达式来分组并将模式替换为

4

1 回答 1

0

感谢@Wiktor Stribiżew 对正则表达式的帮助,我想出了一个解决方案,但我认为它并不像我期望的那样有效。如果您想出更好的解决方案,请随时添加新答案。

val boldRegex = Regex("(?<!\\*)\\*\\*(?!\\*).*?(?<!\\*)\\*\\*(?!\\*)")


@Composable
fun CustomText(text: String, modifier: Modifier = Modifier) {

    var results: MatchResult? = boldRegex.find(text)

    val boldIndexes = mutableListOf<Pair<Int, Int>>()

    val keywords = mutableListOf<String>()

    var finalText = text

    while (results != null) {
        keywords.add(results.value)
        results = results.next()
    }

    keywords.forEach { keyword ->
        val indexOf = finalText.indexOf(keyword)
        val newKeyWord = keyword.removeSurrounding("**")
        finalText = finalText.replace(keyword, newKeyWord)
        boldIndexes.add(Pair(indexOf, indexOf + newKeyWord.length))
    }

    val annotatedString = buildAnnotatedString {
        append(finalText)

        // Add bold style to keywords that has to be bold
        boldIndexes.forEach {
            addStyle(
                style = SpanStyle(
                    fontWeight = FontWeight.Bold,
                    color = Color(0xff64B5F6),
                    fontSize = 15.sp

                ),
                start = it.first,
                end = it.second
            )

        }
    }

    Text(
        modifier = modifier
            .fillMaxWidth()
            .padding(start = 8.dp, end = 8.dp, top = 12.dp, bottom = 12.dp),
        fontSize = 16.sp,
        text = annotatedString
    )
}

我认为它可以在不使用第二个循环的情况下解决,但找不到它。

结果为

Column() {
    CustomText(text = "This is a **bold** text")

    CustomText(
        text = "This is a **bold** text and another **random** value **build** with *regex expression"
    )

    CustomText(
        text = "This is NOT a ****bold** text build with *regex expression"
    )

    CustomText(
        text = "This is NOT a **bold text build with *regex expression"
    )
}

在此处输入图像描述

于 2021-10-20T19:36:38.997 回答