Created
June 24, 2020 08:25
-
-
Save Mojtaba-Shafaei/e74dea9f4b68881743d67ea915b32852 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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