Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create TypeFaceSpan from TypeFace below SDK version 28

Tags:

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?

like image 226
erluxman Avatar asked Mar 27 '19 01:03

erluxman


1 Answers

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) 
like image 59
erluxman Avatar answered Sep 21 '22 08:09

erluxman