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?
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)