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