Created
August 13, 2019 08:47
-
-
Save Terenfear/7fb9f505babd3bfeea59efdbc86b780b to your computer and use it in GitHub Desktop.
This file contains 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
import android.content.Context | |
import android.os.Build | |
import android.text.Html | |
import android.text.SpannableStringBuilder | |
import android.text.Spanned | |
import android.text.SpannedString | |
import android.text.method.LinkMovementMethod | |
import android.text.style.ClickableSpan | |
import android.text.style.URLSpan | |
import android.view.View | |
import android.widget.TextView | |
import androidx.annotation.StringRes | |
import androidx.core.text.TextUtilsCompat | |
/** | |
* Created by Terenfear on 11.02.2019. | |
*/ | |
fun TextView.setTextFromHtml(html: String, onUrlClick: ((url: String) -> Unit)? = null) { | |
val sequence: Spanned = fromHtml(html) | |
if (onUrlClick != null) { | |
text = processLinks(sequence, onUrlClick) | |
movementMethod = LinkMovementMethod.getInstance() | |
} else { | |
text = sequence | |
} | |
} | |
fun fromHtml(html: String): Spanned { | |
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT) | |
} else { | |
Html.fromHtml(html) | |
} | |
} | |
fun toHtml(spanned: Spanned): String { | |
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
Html.toHtml(spanned, Html.FROM_HTML_MODE_COMPACT) | |
} else { | |
Html.toHtml(spanned) | |
} | |
} | |
private fun processLinks( | |
sequence: Spanned, | |
onUrlClick: ((url: String) -> Unit)? | |
): Spanned { | |
val strBuilder = SpannableStringBuilder(sequence) | |
val urls = strBuilder.getSpans(0, sequence.length, URLSpan::class.java) | |
for (span in urls) { | |
makeLinkClickable(strBuilder, span, onUrlClick) | |
} | |
return strBuilder | |
} | |
private fun makeLinkClickable( | |
strBuilder: SpannableStringBuilder, | |
span: URLSpan, | |
onUrlClick: ((url: String) -> Unit)? | |
) { | |
val start = strBuilder.getSpanStart(span) | |
val end = strBuilder.getSpanEnd(span) | |
val flags = strBuilder.getSpanFlags(span) | |
val clickable = object : ClickableSpan() { | |
override fun onClick(view: View) { | |
onUrlClick?.invoke(span.url) | |
} | |
} | |
strBuilder.setSpan(clickable, start, end, flags) | |
strBuilder.removeSpan(span) | |
} | |
fun Context.getText(@StringRes resId: Int, vararg args: Any): CharSequence { | |
val formattedArgs = args.map { TextUtilsCompat.htmlEncode(it.toString()) }.toTypedArray() | |
return fromHtml( | |
String.format( | |
toHtml( | |
SpannedString( | |
getText(resId) | |
) | |
), | |
*formattedArgs | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment