

Exploring text on Canvas using drawText API in Jetpack Compose
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.

Exploring text on Canvas using drawText API in Jetpack Compose

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.drawText
to 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

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.

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

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

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

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
-
8
Exploring Jetpack Compose: @Preview annotation The ability to Preview Jetpack Compose composables within Android...
-
7
Exploring Jetpack Compose: Column The Column Composable provides us with the functionality of displaying a collec...
-
9
Exploring Jetpack Compose: Padding Modifier By hitherejoeIn Android3...
-
5
自定义 View 1-3 drawText() 文字的绘制 这期是 HenCoder 自定义绘制的第三期:文字的绘制。 之前的内容在这里:HenCoder Android 开发进阶 自定义 View 1-1 绘制基础
-
12
Exploring Jetpack Compose: Card The Card composable is a surface that can be used to present content and actions...
-
9
Exploring Jetpack Compose Canvas: the power of drawing
-
5
Jetpack Compose Canvas 无法预览问题 2021-12-08 一般情况下,预览都是直接加上注解,指定一个主题,比如:@Preview(showBackground = true) @Composable fun DefaultPreview() { IceBoxTheme {
-
12
Android Canvas.drawText advertisements I have a view, I'm drawing with the Canvas object in the onDraw(Canvas c...
-
4
Utilising the Canvas in Jetpack Compose to build a SquareSegmentedProgressIndicatorIn recent years, Jetpack Compose has become more and more popular when making Android UI. The reason mainly being that...
-
10
Crafting a Clock in Jetpack Compose with Canvas
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK