Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to outline text in Jetpack Compose

I have a Jetpack Compose Text() element that I'd like to outline in black like so this.
Anyone know how to do this? I've tried using the border() modifier, but that just adds a border around the rectangular area containing the text. I've also tried overlaying two text elements, but that doesn't quite work either.

like image 637
ichen2 Avatar asked Apr 05 '21 18:04

ichen2


People also ask

How do you put an outline on Text?

Select your text or WordArt. Click Home > Text Effects. Click the effect you want. For more choices, point to Outline, Shadow, Reflection, or Glow, and then click the effect you want.

How do I make Text bold in compose?

Android Compose – Change Text to Bold To change font weight of Text composable to Bold, in Android Jetpack Compose, pass FontWeight. Bold for the optional fontWeight parameter of Text composable.

How do I change the Text color in compose?

To change color of Text composable in Android Jetpack Compose, pass a required Color value for the optional color parameter of Text composable.

How do I get TextField value in jetpack compose?

To read value entered in TextField in Android Compose, declare a variable, and assign this variable the value in the TextField whenever there is a change in the value. The same process holds for reading value entered in OutlineTextField composable.

What is textstyle in jetpack compose text?

5. TextStyle in JetPack Compose Text plays important role in mobile/web applications. We can present the details to the user using Text. Let's assume If we use the same font, same color, same size for this entire blog it doesn't look good and users can't able to understand.

What is textfield in jetpack compose?

TextField in Jetpack Compose What's TextField? TextField is a user interface control that is used to allow the user to enter the text. This widget is used to get the data from the user as numbers or text. A simple example of TextField is Login page. We get the username and password using TextField widget. What are options available in TextField?

What are the different types of buttons in jetpack compose?

In general, there are three types of Buttons provided by Material Design Component library for Jetpack Compose. 1. Button (): A normal and regular button 2. OutlinedButton (): Transparent background but with an outline 3. TextButton (): A normal button that appears like a text

How do I style text in the text composable?

The Text composable has multiple optional parameters to style its content. Below, we’ve listed parameters that cover most common use cases with text. To see all the parameters of Text, we recommend you to look at the Compose Text source code. Whenever you set one of these parameters, you’re applying the style to the whole text value.


1 Answers

You can use a Canvas and the drawIntoCanvas function.

Something like:

Canvas(
    modifier = Modifier.fillMaxSize(),
    onDraw = {
                drawIntoCanvas {
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        textPaintStroke
                    )
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        textPaint
                    )
                }
            }
)

with these Paint:

val textPaintStroke = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.STROKE
    textSize = 64f
    color = android.graphics.Color.BLACK
    strokeWidth = 12f
    strokeMiter= 10f
    strokeJoin = android.graphics.Paint.Join.ROUND
}

val textPaint = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.FILL
    textSize = 64f
    color = android.graphics.Color.WHITE
}

enter image description here

like image 70
Gabriele Mariotti Avatar answered Oct 19 '22 05:10

Gabriele Mariotti