LineBreak


When soft wrap is enabled and the width of the text exceeds the width of its container, line breaks are inserted in the text to split it over multiple lines.

There are a number of parameters that affect how the line breaks are inserted. For example, the breaking algorithm can be changed to one with improved readability at the cost of speed. Another example is the strictness, which in some languages determines which symbols can appear at the start of a line.

LineBreak represents a configuration for line breaking, offering several presets for different use cases: Simple, Heading, Paragraph.

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.LineBreak
import androidx.compose.ui.unit.sp

Text(
    text = "Title of an article",
    style = TextStyle(fontSize = 20.sp, lineBreak = LineBreak.Heading)
)

Text(
    text = "A long paragraph in an article",
    style = TextStyle(lineBreak = LineBreak.Paragraph)
)

For further customization, each platform has its own parameters. An example on Android:

import androidx.compose.material.Text
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.LineBreak
import androidx.compose.ui.unit.sp

val customTitleLineBreak =
    LineBreak(
        strategy = LineBreak.Strategy.Simple,
        strictness = LineBreak.Strictness.Loose,
        wordBreak = LineBreak.WordBreak.Default
    )

Text(
    text = "Title of an article",
    style = TextStyle(fontSize = 20.sp, lineBreak = customTitleLineBreak)
)

val defaultStrictnessParagraphLineBreak =
    LineBreak.Paragraph.copy(strictness = LineBreak.Strictness.Default)

Text(
    text = "A long paragraph in an article",
    style = TextStyle(lineBreak = defaultStrictnessParagraphLineBreak)
)

Summary

Nested types

value class LineBreak.Strategy

The strategy used for line breaking.

Describes the strictness of line breaking, determining before which characters line breaks can be inserted.

Describes how line breaks should be inserted within words.

Public companion properties

LineBreak

Looser breaking rules, suitable for short text such as titles or narrow newspaper columns.

Cmn
android
LineBreak

Slower, higher quality line breaking for improved readability.

Cmn
android
LineBreak

Basic, fast line breaking.

Cmn
android
LineBreak

This represents an unset value, a usual replacement for "null" when a primitive value is desired.

Cmn
android

Public constructors

LineBreak(
    strategy: LineBreak.Strategy,
    strictness: LineBreak.Strictness,
    wordBreak: LineBreak.WordBreak
)

This represents a configuration for line breaking on Android, describing Strategy, Strictness, and WordBreak.

android

Public functions

LineBreak
copy(
    strategy: LineBreak.Strategy,
    strictness: LineBreak.Strictness,
    wordBreak: LineBreak.WordBreak
)
android
open String
android

Public companion properties

Heading

val HeadingLineBreak

Looser breaking rules, suitable for short text such as titles or narrow newspaper columns. For longer lines of text, use Paragraph for improved readability.

Paragraph

val ParagraphLineBreak

Slower, higher quality line breaking for improved readability. Suitable for larger amounts of text.

Simple

val SimpleLineBreak

Basic, fast line breaking. Ideal for text input fields, as it will cause minimal text reflow when editing.

Unspecified

val UnspecifiedLineBreak

This represents an unset value, a usual replacement for "null" when a primitive value is desired.

Public constructors

LineBreak

LineBreak(
    strategy: LineBreak.Strategy,
    strictness: LineBreak.Strictness,
    wordBreak: LineBreak.WordBreak
)

This represents a configuration for line breaking on Android, describing Strategy, Strictness, and WordBreak.

Parameters
strategy: LineBreak.Strategy

defines the algorithm that inserts line breaks

strictness: LineBreak.Strictness

defines the line breaking rules

wordBreak: LineBreak.WordBreak

defines how words are broken

Public functions

copy

fun copy(
    strategy: LineBreak.Strategy = this.strategy,
    strictness: LineBreak.Strictness = this.strictness,
    wordBreak: LineBreak.WordBreak = this.wordBreak
): LineBreak

toString

open fun toString(): String

Public properties

strategy

val strategyLineBreak.Strategy

strictness

val strictnessLineBreak.Strictness

wordBreak

val wordBreakLineBreak.WordBreak