如何将Jetpack Compose中的TextStyle转换为android.graphics.Typeface?

14

我需要在Compose中将文本绘制到Canvas上,为此我需要使用具有android.graphics.TypefaceTextPaint

是否有一种简便的方法将Compose TextStyle转换为android.graphics.Typeface

5个回答

12
您可以使用 LocalFontFamilyResolverandroidx.compose.ui.text.TextStyle 对象中解析出 android.graphics.Typeface 对象。
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

1
哦,那看起来真的很有前途! - Bartek Lipinski
如果在TextView上设置这种字体,请考虑将TextStyle的lineHeight和letterSpacing属性也应用到TextView上。 - N1hk

1

目前我找到的唯一解决方法是提供资源:

val textTypeface: android.graphics.Typeface? =
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) LocalContext.current.resources.getFont(R.font.quicksand_light) else null

然而,如果 Android 版本低于 Android Oreo,我不知道如何提供字体,因此我将回退到默认字体。


1
谢谢您的建议!不幸的是,这无法转换TextStyle(其中包含有关font的内部信息)。 - Bartek Lipinski

0

我遇到了和你一样的问题:想要在一个在我的Compose主题中定义的TextStyle中向Canvas绘制文本。

我发现可以使用androidx.compose.ui.text.Paragraph.paint(canvas)将文本绘制到Canvas上。

这本身没有办法为绘画工作设置偏移量,所以您可以使用drawContext.canvas.nativeCanvas.withTranslation()将绘画移动到指定的偏移量。

val paragraph = Paragraph(
  text = "Hello",
  style = MaterialTheme.typography.titleLarge,
  constraints = Constraints(),
  density = LocalDensity.current,
  fontFamilyResolver = LocalFontFamilyResolver.current,
)
val colorOnSurface = MaterialTheme.colorScheme.onSurface

Canvas(
  modifier = Modifier.fillMaxSize()
) {
  //
  drawContext.canvas.nativeCanvas.withTranslation(
    100f,
    100f
  ) {
      paragraph.paint(
        canvas = drawContext.canvas,
        color = colorOnSurface,
      )
  }
}

这看起来相当可疑,但这是我最好的办法 ¯_(ツ)_/¯。


0

试一下这个

    val textPaint = TextPaint()
    val context = LocalContext.current
    Canvas(modifier = Modifier.fillMaxWidth()) {
        textPaint.apply {
            typeface = Typeface.createFromAsset(context.assets, "fonts/yourfont.ttf")
                ...
        }
        drawContext.canvas.nativeCanvas.drawText("test", yourStart, yourEnd, X, Y, textPaint)
    }

2
感谢您的回复!不幸的是,这并没有回答问题 - 提供一种将“TextStyle”转换为“Typeface”的方法。 - Bartek Lipinski

-1

要在画布上绘制文本,您可以像这样操作

@Composable
fun DrawText() {
    val paint = Paint().asFrameworkPaint()
    Canvas(modifier = Modifier.fillMaxSize()) {
        paint.apply {
            isAntiAlias = true
            textSize = 24f
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD) // your typeface

            //other methods like color, dither, fontMetrics, shadow etc...are also available 

        }

        drawIntoCanvas {
            it.nativeCanvas.drawText("Hello World", size.width/2, size.height/2, paint)
        }
    }
}

我认为将TextStyle(compose库)转换为typeface会很麻烦,因为Android没有提供支持。如果你想在画布上绘制文本,我认为这已经足够了。


2
感谢您的回复!不幸的是,这并没有回答我的问题。我知道如何在画布上绘制文本,我正在寻找一种将“TextStyle”转换为“Typeface”的方法。 - Bartek Lipinski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接