In order to add a border to any side of a UI component
in Jetpack Compose, you can extend the Modifier class with
your own custom border functions that simply draw a custom line.

Here's how you can achieve this:

**For the top border:**

fun Modifier.topBorder(strokeWidth: Dp, color: Color) = composed(
    factory = {
        val density = LocalDensity.current    
        val strokeWidthPx = density.run { strokeWidth.toPx() }

        Modifier.drawBehind {
            val width = size.width
            val strokeWidthHalf = strokeWidthPx / 2

            drawLine(
                color = color,
                start = Offset(x = 0f, y = strokeWidthHalf),
                end = Offset(x = width, y = strokeWidthHalf),
                strokeWidth = strokeWidthPx
            )
        }
    }
)

**For the bottom border:**

fun Modifier.bottomBorder(strokeWidth: Dp, color: Color) = composed(
    factory = {
        val density = LocalDensity.current    
        val strokeWidthPx = density.run { strokeWidth.toPx() }

        Modifier.drawBehind {
            val width = size.width
            val height = size.height - strokeWidthPx / 2

            drawLine(
                color = color,
                start = Offset(x = 0f, y = height),
                end = Offset(x = width , y = height),
                strokeWidth = strokeWidthPx
            )
        }
    }
)

**For the left border:**

fun Modifier.leftBorder(strokeWidth: Dp, color: Color) = composed(
    factory = {
        val density = LocalDensity.current    
        val strokeWidthPx = density.run { strokeWidth.toPx() }

        Modifier.drawBehind {
            val height = size.height
            val strokeWidthHalf = strokeWidthPx / 2

            drawLine(
                color = color,
                start = Offset(x = strokeWidthHalf, y = 0f),
                end = Offset(x = strokeWidthHalf, y = height),
                strokeWidth = strokeWidthPx
            )
        }
    }
)

**For the right border:**

fun Modifier.rightBorder(strokeWidth: Dp, color: Color) = composed(
    factory = {
        val density = LocalDensity.current    
        val strokeWidthPx = density.run { strokeWidth.toPx() }

        Modifier.drawBehind {
            val width = size.width - strokeWidthPx / 2
            val height = size.height

            drawLine(
                color = color,
                start = Offset(x = width, y = 0f),
                end = Offset(x = width, y = height),
                strokeWidth = strokeWidthPx
            )
        }
    }
)

With these extension functions, you can easily add a border
to any side of your UI component by just appending the function
to the component's modifier. For example:

Text(
    text = "Hello, World!",
    modifier = Modifier.rightBorder(2.dp, Color.Black)
)

This will draw a black border with a thickness of 2.dp on the right side of the text.

Kotlin相关代码片段

run function in companion object kotlin

kotlin compiler

preety print a data class in kotlin

kotlin time taken

check internet reachable android kotlin

check network status android kotlin

check internet available on android kotlin

how to read a exel file in kotlin

square root in kotlin

convert list to string kotlin

splash screen in react native kotlin

view elevation android kotlin

abstraction in kotlin

singleton object in kotlin

round up in kotlin

keyboard options jetpack compose kotlin

Unresolved reference: Transformations

Fibonacci using an infinite sequence

options menu without an action bar android kotlin

kotlin when statement

Kotlin okhttp

compose, kotlin

import a external jar gradle kotlin

use viewbinding android

check if string is url android kotlin

inherit from button kotlin

inherit from view kotlin

give me above code in kotlin

check jks password

customSpinnerAdapter

Jetpack Compose make image fullsize

kotlin spring boot run code after component init

update kotlin gradle plugin

dictionary in kotlin

compare two numeric string values

kotlin create function

send method as parameter kotlin

pass class as parameter kotlin

set text to edittext android kotlin

minus kotlin

kotlin pair triple quadruple

how to validate email kotlin

Jetpack Compose custom switch

get string always defaulting

Jetpack Compose one sided stroke

android studio int to long

Android Kotlin Compose bottom bar

was ist bei der if anweisung dreimal === in kotlin

mapstruct to dto all fields null

set margins kotlin

kotlin serialization ignore field

kotlin interview questions android

kotlin list to string with separator

kotlin kapt plugin android

kotlin operators

kotlin variables

kotlin data types

kotlin comments

kotlin output

kotlin hello world 1.3

kotlin kotlin ide

can by lazy used with var in kotlin

open link in browser kotlin

how to kotlin get last string

how to write event calendar android kotlin

how to get .push string firebase kotlin

how to play background music in kotlin

room db android default value

Retrofit With MVI Architecture using Kotlin

viewbinding

bootstrap5 columns

check if variable is initialized kotlin

kotlin find max value in list of objects

kotlin time hours

how to create kotlin project in android studio

recyclerview kotlin grid layout

android studio change activity kotlin

create viewmodel instance android kotlin

context in kotlin

android kotlin local currency code and symbol