1

I have some path and some text and concrete paint to draw it on canvas by drawTextOnPath method. How to compute how much length it will take on the path? I need it to compute textScaleX value.

1 Answers1

0

Try Paint.getTextWidths().

override fun onDraw(canvas: Canvas?) {

    :
   
    val message = "Hello World!"

    val paintText = Paint().apply {
        color = Color.BLACK
        textSize = 3.0F * pathLength / message.length
    }

    val advanceWidths = FloatArray(message.length)
    paintText.getTextWidths(message, advanceWidths)
    val textWidth = advanceWidths.sum()

    paintText.textScaleX = pathLength / textWidth

    canvas?.drawTextOnPath(message, path, 0.0F, 0.0F, paintText)
}
ardget
  • 2,561
  • 1
  • 5
  • 4