Create TypeFaceSpan from TypeFace below SDK version 28

3.9k Views Asked by At

I found a way to create TypeFaceSpan from TypeFace like this :

fun getTypeFaceSpan(typeFace:TypeFace) = TypeFaceSpan(typeFace)

But this API is allowed only in API level >= 28 . Any Compat libray to achieve this below 28?

2

There are 2 best solutions below

0
On BEST ANSWER

TypeFaceSpan is a MetricAffectingSpan. So even if there is not any exact way to get TypeFaceSpan from Span, we can make CustomTypeFaceSpan like below and use it in place of TypeFaceSpan.

class CustomTypefaceSpan(private val typeface: Typeface?) : MetricAffectingSpan() {
    override fun updateDrawState(paint: TextPaint) {
        paint.typeface = typeface
    }

    override fun updateMeasureState(paint: TextPaint) {
        paint.typeface = typeface
    }
}

And Use it like this :

val typeFaceSpan = CustomTypefaceSpan(typeface)
0
On

Inspired on the answer from @erluxman and combining with Kotlin's extension functions:

fun Typeface.getTypefaceSpan(): MetricAffectingSpan {
    return if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.P) typefaceSpanCompatV28(this) else CustomTypefaceSpan(this)
}

@TargetApi(Build.VERSION_CODES.P)
private fun typefaceSpanCompatV28(typeface: Typeface) =
    TypefaceSpan(typeface)


private class CustomTypefaceSpan(private val typeface: Typeface?) : MetricAffectingSpan() {
    override fun updateDrawState(paint: TextPaint) {
        paint.typeface = typeface
    }

    override fun updateMeasureState(paint: TextPaint) {
        paint.typeface = typeface
    }
}

then you can use it like this:

typeface.getTypefaceSpan()