Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert TextStyle from Jetpack Compose to android.graphics.Typeface?

I need to draw text onto Canvas in Compose, for this purpose I need a TextPaint with android.graphics.Typeface.

Is there a way to easily convert Compose TextStyle to a android.graphics.Typeface?

like image 800
Bartek Lipinski Avatar asked Dec 31 '25 13:12

Bartek Lipinski


1 Answers

You can resolve android.graphics.Typeface object from a androidx.compose.ui.text.TextStyle object using LocalFontFamilyResolver.

val style: TextStyle = MaterialTheme.typography.body1
val resolver: FontFamily.Resolver = LocalFontFamilyResolver.current

val typeface: Typeface = remember(resolver, style) {
    resolver.resolve(
        fontFamily = style.fontFamily,
        fontWeight = style.fontWeight ?: FontWeight.Normal,
        fontStyle = style.fontStyle ?: FontStyle.Normal,
        fontSynthesis = style.fontSynthesis ?: FontSynthesis.All,
    )
}.value as Typeface
like image 71
Jaya Surya Thotapalli Avatar answered Jan 03 '26 11:01

Jaya Surya Thotapalli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!