Skip to main content

Command Palette

Search for a command to run...

How to Apply Stroke Effects to Text in Jetpack Compose

Exploring DrawStyle API for Text Stroke Effects in Jetpack Compose

Published
2 min read
How to Apply Stroke Effects to Text in Jetpack Compose
C

Unless you’re a Multimillion or a Billion dollar company, you probably don’t have a multimillion-dollar ad budget or professional Spinners. Your product needs to stand out on its own merits like App Quality, Performance, UI design, and User Experience. Most companies don't care about you, your product, and your vision or dreams. They don't give a damn about either their work helped you to get more business, revenue, users, or solving a problem. That's where CANOPAS comes into the picture. Whether you have a GREAT IDEA and you want to turn it into a DIGITAL PRODUCT. OR You need a team that can turn your NIGHTMARES into SWEET DREAMS again by improving your existing product. We help Entrepreneurs, startups, and small companies to bring their IDEA to LIFE by developing digital products for their business. We prefer using Agile and Scrum principles in project management for flexibility and rapid review cycles. We are not bound by technology. We will learn new technology if it significantly improves the performance of your app. We will solve your tech-related problems even though we are not THE EXPERT in it. And we've done it multiple times in the last 7 years. In the last seven years, we helped... A STARTUP to expand its users from 2500 to over 100000 by developing mobile apps for them. An enterprise client to redevelop their app that has 1M+ monthly paid users and 10M+ app downloads. Another enterprise client(5M+ app downloads in each store) to fix bugs and broken parts in the app and as a result, they had over 98% crash-free users. We offer a 100% MONEY BACK GUARANTEE if you don't like our work. No questions asked. Visit : https://canopas.com/blog

Jetpack Compose recently added many new customization options to TextStyle and TextStyle.drawStyle is one of them. Using this, we can apply stunning stroke effects to text in Jetpack Compose.

In this blog post, we’ll explore how to use DrawStyle API to create unique and creative stroke effects for texts.

Table of Contents

  • Draw Simple outlined text

  • Draw round-corner outlined text

  • Draw dash stroke

  • Draw the outline with multiple path effects

  • Draw the double outlines

  • Draw Gradient outline

  • Draw outlines with shapes

  • Example 1 - Curved outline

  • Example 2 - Heart shaped outline

  • Conclusion

Let's get started.

We’ll use Stroke drawStyle, which provides information for drawing content with a stroke.
Let’s first look at what we have in Stroke API,

class Stroke(
    val width: Float = 0.0f,
    val miter: Float = DefaultMiter,
    val cap: StrokeCap = DefaultCap,
    val join: StrokeJoin = DefaultJoin,
    val pathEffect: PathEffect? = null
) : DrawStyle() { }
  • width — Configure the width of the stroke in pixels

  • miter — Set the stroke miter value. This is used to control the behavior of miter joins when the joins angle is sharp. This value must be >= 0

  • cap — Return the paint’s Cap, controlling how the start and end of stroked lines and paths are treated. The default is StrokeCap.Butt

  • join — Set the treatment where lines and curve segments join on a stroked path. The default is StrokeJoin.Miter

  • pathEffect — Effect to apply to the stroke, null indicates a solid stroke line is to be drawn

For all the examples with source code, check out canopas blog.