fix line breaks abscence in spanned text
This commit is contained in:
parent
f2538550a9
commit
43fd0f6f40
|
|
@ -13,8 +13,9 @@ fun String.getSpannedTextWithUrls(
|
|||
removeUnderline: Boolean = true,
|
||||
flags: Int = HtmlCompat.FROM_HTML_MODE_COMPACT
|
||||
): Spanned {
|
||||
|
||||
val spannableText = SpannableString(HtmlCompat.fromHtml(this, flags))
|
||||
// HtmlCompat.fromHtml doesn't respect line breaks
|
||||
val text = this.replace(lineBreakRegex, "<br/>")
|
||||
val spannableText = SpannableString(HtmlCompat.fromHtml(text, flags))
|
||||
|
||||
// Linkify removes all previous URLSpan's, we need to save all created spans for reapply after Linkify
|
||||
val spans = spannableText.getUrlSpans()
|
||||
|
|
@ -38,6 +39,10 @@ fun String.getSpannedTextWithUrls(
|
|||
return spannableText
|
||||
}
|
||||
|
||||
private val lineBreakRegex by lazy(LazyThreadSafetyMode.NONE) {
|
||||
"\r?\n".toRegex()
|
||||
}
|
||||
|
||||
private fun SpannableString.getUrlSpans() = getSpans(0, length, URLSpan::class.java)
|
||||
.map { UrlSpanWithBorders(it, this.getSpanStart(it), this.getSpanEnd(it)) }
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue