Android 12 Developer Preview is here! Try it out, and give us your feedback!

ParagraphStyle

@Immutable class ParagraphStyle
kotlin.Any
   ↳ androidx.compose.ui.text.ParagraphStyle

Paragraph styling configuration for a paragraph. The difference between SpanStyle and ParagraphStyle is that, ParagraphStyle can be applied to a whole Paragraph while SpanStyle can be applied at the character level. Once a portion of the text is marked with a ParagraphStyle, that portion will be separated from the remaining as if a line feed character was added.

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextIndent

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.material.Text
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextIndent

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)
    }
)

Summary

Public constructors
<init>(textAlign: TextAlign? = null, textDirection: TextDirection? = null, lineHeight: TextUnit = TextUnit.Unspecified, textIndent: TextIndent? = null)

Paragraph styling configuration for a paragraph.

Public methods
ParagraphStyle
copy(textAlign: TextAlign? = this.textAlign, textDirection: TextDirection? = this.textDirection, lineHeight: TextUnit = this.lineHeight, textIndent: TextIndent? = this.textIndent)

operator Boolean
equals(other: Any?)

Int

ParagraphStyle
merge(other: ParagraphStyle? = null)

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

operator ParagraphStyle

Plus operator overload that applies a merge.

String

Properties
TextUnit

Line height for the Paragraph in TextUnit unit, e.

TextAlign?

The alignment of the text within the lines of the paragraph.

TextDirection?

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

TextIndent?

The indentation of the paragraph.

Public constructors

<init>

ParagraphStyle(
    textAlign: TextAlign? = null,
    textDirection: TextDirection? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    textIndent: TextIndent? = null)

Paragraph styling configuration for a paragraph. The difference between SpanStyle and ParagraphStyle is that, ParagraphStyle can be applied to a whole Paragraph while SpanStyle can be applied at the character level. Once a portion of the text is marked with a ParagraphStyle, that portion will be separated from the remaining as if a line feed character was added.

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextIndent

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.material.Text
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextIndent

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)
    }
)
Parameters
textAlign: TextAlign? = null The alignment of the text within the lines of the paragraph.
textDirection: TextDirection? = null The algorithm to be used to resolve the final text direction: Left To Right or Right To Left.
textIndent: TextIndent? = null The indentation of the paragraph.
lineHeight: TextUnit = TextUnit.Unspecified Line height for the Paragraph in TextUnit unit, e.g. SP or EM.

Public methods

copy

fun copy(
    textAlign: TextAlign? = this.textAlign,
    textDirection: TextDirection? = this.textDirection,
    lineHeight: TextUnit = this.lineHeight