kotlincode-duplicationkotlin-extensionspannablestringbuilder

Kotlin - Create custom ext function for SpannableStringBuilder without duplicate arguments when declaring start, end & flasg for setSpans()


this is MainActivity.kt before

var spannable = SpannableStringBuilder("$noColorText$coloredText")
spannable.setSpan(
    ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen)),
    noColorText.length, spannable.length,
    Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
spannable.setSpan(
    StyleSpan(BOLD),
    noColorText.length, spannable.length,
    Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
textView.text = spannable

Here's my approach so far.

Extension.kt

// TODO: e.g: "string".putSpans(start, end, flags) { ForgroundColorSpan(color), StyleSpan(BOLD) }
fun String.putSpans(vararg flags: Int.() -> Unit, spanBuilder: SpannableStringBuilder.() -> Unit):
    SpannableStringBuilder = SpannableStringBuilder(this).apply(spanBuilder)

MainActivity.kt

// TODO: Change SpannableBuilder to be modular (without, reinput duplicate args)
val resultSpan = "$noColorText$coloredText ".putSpans {
    setSpan(ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen)),
        noColorText.length, this.length, // this is duplicate
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE) // this is duplicate
    setSpan(StyleSpan(BOLD),
        noColorText.length, this.length, // this is duplicate
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE) // this is duplicate
    }

textView.text = resultSpan

Is this possible to create extension like this

"string".putSpans(start, end, flags) { ForgroundColorSpan(color), StyleSpan(BOLD) }

so we don't have to use duplicate start, end, also flags argument, but open for modification, e.g:

"string".putSpans(start, end, flags) { // for default value
 span(ForgroundColorSpan(color), diffStart, diffEnd), 
 span(StyleSpan(BOLD), diffFlags) 
}

Solution

  • You can use extensions included in core-ktx which simplify using, more specifically, building SpannedString in kotlin as so:

    buildSpannedString {
        bold {
            append("hitherejoe")
        }
    }
    

    I guess you would use it like this:

    buildSpannedString {
        bold {
            inSpans(ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen))) {
                append("string")
            }
        }
    }
    

    See androidx.text package for reference.

    I took the example from this Medium post by Joe Birch.