Mohamed Ibrahim
09/10/2019, 9:59 AMfun String.applySpans(vararg pairs: Pair<String, Any>): Spannable {
val spannable = SpannableStringBuilder(this)
pairs.forEach {
val start = this.indexOf(it.first)
val end = start + it.first.length
spannable.setSpan(it.second, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
}
return spannable
}
this is the working version in the test sample
val bold = StyleSpan(Typeface.BOLD)
val italic = StyleSpan(Typeface.BOLD_ITALIC)
val string = getString(R.string.hello_message, "John", "Wick")
testTextView.text = string.applySpans(
"John" to bold,
"Wick" to italic
)
this is the code with the problem
is AlternateSearchError.NotDeliveringToArea -> {
searchErrorView.errorAction.visible()
val message = getString(R.string.not_delivery_to_this_area, searchError.restaurant, searchError.area)
searchErrorView.errorMessage.text = message.applySpans(
searchError.restaurant to bold,
searchError.area to bold
)
}