androidkotlinspannablestringbuilder

How would I scale multiple spans using SpannableStringBuilder?


I have this working, but redundant, code:

feedbackView.text = SpannableStringBuilder()
    .scale(.6f) { italic { append(getString(R.string.suggestion_prefix)) } }
    .scale(.6f) { append("\n\n") }
    .scale(.6f) { bold { append(s) } }

How would I refactor it so there is only one call to .scale()?

When I try this, only the first string is scaled:

feedbackView.text = SpannableStringBuilder()
    .scale(.6f, { italic { append(getString(R.string.suggestion_prefix)) } })
    .append("\n\n")
    .bold { append(s) }

I have not been able to figure out the syntax to include everything in the lambda argument to scale().


Solution

  • You can put everything inside one scale lambda.

    feedbackView.text = SpannableStringBuilder()
        .scale(.6f) {
            italic { append(getString(R.string.suggestion_prefix)) }
            .append("\n\n")
            .bold { append(s) }
         }