【发布时间】:2018-02-18 21:37:25
【问题描述】:
我正在创建自己的 MarkdownTextView。
在这个特定的例子中,我正在筛选一段文本,解析出斜体标签示例:
Here is text, and *here is italic text*, and maybe *more* italic text
我有一个 regex 函数 可以为我进行筛选:
(\*[^*])(.*?)([^*]\*)
以下是我使用 替换所有斜体 sn-ps 的代码:
val commentBody = "Here is text, and *here is italic text*, and maybe *more* italic text"
val check = "(\*[^*])(.*?)([^*]\*)".toRegex()
val newSpan = SpannableString(commentBody.replace(check, { result ->
val innerSpan = SpannableString(result.value.substring(1, result.value.length - 1))
innerSpan.setSpan(StyleSpan(Typeface.ITALIC), 0, innerSpan.length, spanFlag)
return@replace innerSpan
}))
我的正则表达式工作正常,并且
Here is text, and *here is italic text*, and maybe *more* italic text
正确转换为显示
Here is text, and here is italic text, and maybe more italic text
但是没有任何东西是斜体的。我对此进行了调试,它证实了我的恐惧,即在该转换中设置斜体跨度并使用它来设置我的新可跨度字符串时,我会丢失所有这些跨度。
有什么想法吗?
【问题讨论】:
-
你有没有找到一个好的解决方案?
标签: android kotlin spannablestring