Skip to content

Instantly share code, notes, and snippets.

@Mojtaba-Shafaei
Created June 24, 2020 08:25
Show Gist options
  • Save Mojtaba-Shafaei/e74dea9f4b68881743d67ea915b32852 to your computer and use it in GitHub Desktop.
Save Mojtaba-Shafaei/e74dea9f4b68881743d67ea915b32852 to your computer and use it in GitHub Desktop.
package com.mabnadp.rahavard365.utils
import android.graphics.Paint
import android.graphics.Typeface
import android.text.TextPaint
import android.text.style.TypefaceSpan
class CustomTypefaceSpan(private val newType: Typeface) : TypefaceSpan(null) {
override fun updateDrawState(ds: TextPaint) {
applyCustomTypeFace(ds, newType)
}
override fun updateMeasureState(paint: TextPaint) {
applyCustomTypeFace(paint, newType)
}
private fun applyCustomTypeFace(paint: Paint, tf: Typeface) {
val oldStyle: Int
val old = paint.typeface
oldStyle = old?.style ?: 0
val fake = oldStyle and tf.style.inv()
if (fake and Typeface.BOLD != 0) {
paint.isFakeBoldText = true
}
if (fake and Typeface.ITALIC != 0) {
paint.textSkewX = -0.25f
}
paint.typeface = tf
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment