8

Exploring text on Canvas using drawText API in Jetpack Compose

 2 years ago
source link: https://blog.canopas.com/exploring-text-on-canvas-using-drawtext-api-in-jetpack-compose-402e1285935c
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Exploring text on Canvas using drawText API in Jetpack Compose

1*rel8AZdF8eQ0-jM49FTnVw.png
Designed By Canopas

Before Compose 1.3.0, there was no drawText(). We couldn’t draw text directly on the Jetpack Compose canvas, we had to use android native canvas canvas.nativeCanvas.drawTextto draw text.

Recent release of Jetpack Compose 1.3.0 introduces many new APIs, and Text on Canvas is one of them.

In this article, we’ll explore the new DrawScope.drawText() API. Note that this API is still in the experimental state and it is likely to change in the future.

Here’s the full source code if you need it.

Let’s get started...

Before we dive deep into the new API let’s see the old approach to drawing the text.

Here’s the code snippet

And the result

1*0_pxW6E94xUWC_6H3qAa-g.png

Now it’s time to explore DrawScope.drawText

drawText function has 4 overloads, Let’s see them one by one with examples.

The First two overload takes TextMeasure in an argument, while the other two variant takes TextLayoutResult with other configuration parameters.

Draw text using a TextMeasurer

TextMeasure is a new Experimental API. TextMeasureisresponsible for measuring text layout. To learn more about TextMeasure, check out the official API doc.

With this overload function, we can draw styled text on canvas. With rememberTextMeasurer() we can create an instance of TextMeasurer

rememberTextMeasurer() takes cacheSize as parameters. cacheSize defines the capacity of the internal cache inside TextMeasurer, which means the number of unique text layout inputs that are measured.

Let’s see the example now

Here we have created textMeasure instance by rememberTextMeasurer() and annotated string with style also set up the topLeft point. Also to style our text we’ve used Brush API to apply gradient coloring.

1*AJ1Mo6jKhxeZGcm4nJy8sQ.png

Nice colorful text on canvas 🤩.

Now let’s see the second overload function

This draw function supports only one text style and async font loading.

The signature is the same as the previous overload function, except for the text parameters which take the string instead of Annotated string.

And the output

1*LVyyXS4LqEHagat1Xoxodw.png

Now let’s see the remaining two overloads which draw an existing text layout on canvas

Draw text using a TextLayoutResult

TextLayoutResult can be generated by textMeasurer.measure()

Both overload functions are the same, except for color and brush parameters to color the text.

Let’s first see how to create TextLayoutResult by TextMeasure.measure() Here we’ll use LayoutModifier to create TextLayoutResult.

here, textMeasure.measure takes an annotated string and other configurations as an argument. Currently, there’s no way to pass a simple text string.

Now let’s use the above created textLayoutResult to draw text

Along with textLayoutResult we have also customized our text. Let’s see the result

1*T_dc1Tvckxf-YBPTsMJnAQ.png

Easy. isn’t it?

Like Text composable, you can decorate and customize text according to your requirements

For example, you can set overflow when text is too long to fit

Here text is ellipsed to fit in the container

1*eDR7prB7l5J-VvTVUSJsqQ.png

To Conclude

That’s it for today, hope you have a basic idea of drawText API. It’s effortless to use. You can do lots of customization to decorate your text on canvas. Let’s wait for Experimental Tag to be removed from API, till that keep exploring it 🍻.

The complete source code of the above examples is available on Github.

Hope you found this blog post useful.

Thank you!!

If you like what you read, be sure to 👏👏👏 below — as a writer it means the world!

Follow Canopasto get updates on interesting tech articles!


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK