ParagraphStyle


Paragraph styling configuration.

Defines styling parameters that apply to a whole paragraph (e.g., alignment, line height).

In contrast to SpanStyle which applies at character level, ParagraphStyle separates the marked text into a new paragraph, as if a line feed character was inserted.

import androidx.compose.material3.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.unit.sp

val textStyle =
    TextStyle(
        textAlign = TextAlign.Justify,
        lineHeight = 20.sp,
        textIndent = TextIndent(firstLine = 14.sp, restLine = 3.sp),
    )
Text(
    text =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
            "incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis " +
            "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
    style = textStyle,
)
import androidx.compose.material3.Text
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.unit.sp

val text =
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " +
        "incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis " +
        "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

val paragraphStyle1 = ParagraphStyle(textIndent = TextIndent(firstLine = 14.sp))
val paragraphStyle2 = ParagraphStyle(lineHeight = 30.sp)

Text(
    text =
        buildAnnotatedString {
            append(text)
            addStyle(paragraphStyle1, 0, text.indexOf('\n') + 1)
            addStyle(paragraphStyle2, text.indexOf('\n') + 1, text.length)
        }
)
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.unit.sp

with(AnnotatedString.Builder()) {
    // push a ParagraphStyle to be applied to any appended text after this point.
    pushStyle(ParagraphStyle(lineHeight = 18.sp))
    // append a paragraph which will have lineHeight 18.sp
    append("Paragraph One\n")
    // pop the ParagraphStyle
    pop()
    // append new paragraph, this paragraph will not have the line height defined.
    append("Paragraph Two\n")

    toAnnotatedString()
}

Summary

Public constructors

ParagraphStyle(
    textAlign: TextAlign,
    textDirection: TextDirection,
    lineHeight: TextUnit,
    textIndent: TextIndent?,
    platformStyle: PlatformParagraphStyle?,
    lineHeightStyle: LineHeightStyle?,
    lineBreak: LineBreak,
    hyphens: Hyphens,
    textMotion: TextMotion?
)
Cmn

Public functions

ParagraphStyle
copy(
    textAlign: TextAlign,
    textDirection: TextDirection,
    lineHeight: TextUnit,
    textIndent: TextIndent?,
    platformStyle: PlatformParagraphStyle?,
    lineHeightStyle: LineHeightStyle?,
    lineBreak: LineBreak,
    hyphens: Hyphens,
    textMotion: TextMotion?
)
Cmn
open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
ParagraphStyle

Returns a new paragraph style that is a combination of this style and the given other style.

Cmn
operator ParagraphStyle

Plus operator overload that applies a merge.

Cmn
open String
Cmn

Public properties

Hyphens?

This property is deprecated. Kept for backwards compatibility.

Cmn
LineBreak?

This property is deprecated. Kept for backwards compatibility.

Cmn
TextAlign?

This property is deprecated. Kept for backwards compatibility.

Cmn
TextDirection?

This property is deprecated. Kept for backwards compatibility.

Cmn
Hyphens

hyphenation configuration.

Cmn
LineBreak

line breaking rules.

Cmn
TextUnit

line height

Cmn
LineHeightStyle?

line height distribution configuration

Cmn
PlatformParagraphStyle?

platform-specific parameters

Cmn
TextAlign

alignment of the text within the lines of the paragraph.

Cmn
TextDirection

algorithm used to resolve the final text direction: Left To Right or Right To Left.

Cmn
TextIndent?

paragraph indentation

Cmn
TextMotion?

character placement optimization.

Cmn

Public constructors

ParagraphStyle

ParagraphStyle(
    textAlign: TextAlign = TextAlign.Unspecified,
    textDirection: TextDirection = TextDirection.Unspecified,
    lineHeight: TextUnit = TextUnit.Unspecified,
    textIndent: TextIndent? = null,
    platformStyle: PlatformParagraphStyle? = null,
    lineHeightStyle: LineHeightStyle? = null,
    lineBreak: LineBreak = LineBreak.Unspecified,
    hyphens: Hyphens = Hyphens.Unspecified,
    textMotion: TextMotion? = null
)
Parameters
textAlign: TextAlign = TextAlign.Unspecified

alignment of the text within the lines of the paragraph.

textDirection: TextDirection = TextDirection.Unspecified

algorithm used to resolve the final text direction: Left To Right or Right To Left.

lineHeight: TextUnit = TextUnit.Unspecified

line height

textIndent: TextIndent? = null

paragraph indentation

platformStyle: PlatformParagraphStyle? = null

platform-specific parameters

lineHeightStyle: LineHeightStyle? = null

line height distribution configuration

lineBreak: LineBreak = LineBreak.Unspecified

line breaking rules.

hyphens: Hyphens = Hyphens.Unspecified

hyphenation configuration.

textMotion: TextMotion? = null

character placement optimization.

Public functions

copy

fun copy(
    textAlign: TextAlign = this.textAlign,
    textDirection: TextDirection = this.textDirection,
    lineHeight: TextUnit = this.lineHeight,
    textIndent: TextIndent? = this.textIndent,
    platformStyle: PlatformParagraphStyle? = this.platformStyle,
    lineHeightStyle: LineHeightStyle? = this.lineHeightStyle,
    lineBreak: LineBreak = this.lineBreak,
    hyphens: Hyphens = this.hyphens,
    textMotion: TextMotion? = this.textMotion
): ParagraphStyle

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

merge

fun merge(other: ParagraphStyle? = null): ParagraphStyle

Returns a new paragraph style that is a combination of this style and the given other style.

If the given paragraph style is null, returns this paragraph style.

Parameters
other: ParagraphStyle? = null

style to merge

plus

operator fun plus(other: ParagraphStyle): ParagraphStyle

Plus operator overload that applies a merge.

toString

open fun toString(): String

Public properties

deprecated_boxing_hyphens

val deprecated_boxing_hyphensHyphens?

deprecated_boxing_lineBreak

val deprecated_boxing_lineBreakLineBreak?

deprecated_boxing_textAlign

val deprecated_boxing_textAlignTextAlign?

deprecated_boxing_textDirection

val deprecated_boxing_textDirectionTextDirection?

hyphens

val hyphensHyphens

hyphenation configuration.

lineBreak

val lineBreakLineBreak

line breaking rules.

lineHeight

val lineHeightTextUnit

line height

lineHeightStyle

val lineHeightStyleLineHeightStyle?

line height distribution configuration

platformStyle

val platformStylePlatformParagraphStyle?

platform-specific parameters

textAlign

val textAlignTextAlign

alignment of the text within the lines of the paragraph.

textDirection

val textDirectionTextDirection

algorithm used to resolve the final text direction: Left To Right or Right To Left.

textIndent

val textIndentTextIndent?

paragraph indentation

textMotion

val textMotionTextMotion?

character placement optimization.