Google は、黒人コミュニティに対する人種平等の促進に取り組んでいます。取り組みを見る

SpanStyle

@Immutable data class SpanStyle
kotlin.Any
   ↳ androidx.compose.ui.text.SpanStyle

Styling configuration for a text span. This configuration only allows character level styling, in order to set paragraph level styling such as line height, or text alignment please see ParagraphStyle.

import androidx.compose.material.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.text.buildAnnotatedString

Text(
    fontSize = 16.sp,
    text = buildAnnotatedString {
        withStyle(style = SpanStyle(color = Color.Red)) {
            append("Hello")
        }
        withStyle(SpanStyle(color = Color.Blue)) {
            append(" World")
        }
    }
)
import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder("Hello")) {
    // push green text style so that any appended text will be green
    pushStyle(SpanStyle(color = Color.Green))
    // append new text, this text will be rendered as green
    append(" World")
    // pop the green style
    pop()
    // append a string without style
    append("!")
    // then style the last added word as red, exclamation mark will be red
    addStyle(SpanStyle(color = Color.Red), "Hello World".length, this.length)

    toAnnotatedString()
}

Summary

Public constructors
<init>(color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontWeight: FontWeight? = null, fontStyle: FontStyle? = null, fontSynthesis: FontSynthesis? = null, fontFamily: FontFamily? = null, fontFeatureSettings: String? = null, letterSpacing: TextUnit = TextUnit.Unspecified, baselineShift: BaselineShift? = null, textGeometricTransform: TextGeometricTransform? = null, localeList: LocaleList? = null, background: Color = Color.Unspecified, textDecoration: TextDecoration? = null, shadow: Shadow? = null)

Styling configuration for a text span.

Public methods
SpanStyle
merge(other: SpanStyle? = null)

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

operator SpanStyle
plus(other: SpanStyle)

Plus operator overload that applies a merge.

Properties
Color

The background color for the text.

BaselineShift?

The amount by which the text is shifted up from the current baseline.

Color

The text color.

FontFamily?

The font family to be used when rendering the text.

String?

The advanced typography settings provided by font.

TextUnit

The size of glyphs (in logical pixels) to use when painting the text.

FontStyle?

The typeface variant to use when drawing the letters (e.

FontSynthesis?

Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided custom font family.

FontWeight?

The typeface thickness to use when painting the text (e.

TextUnit

The amount of space (in em) to add between each letter.

LocaleList?

The locale list used to select region-specific glyphs.

Shadow?

The shadow effect applied on the text.

TextDecoration?

The decorations to paint on the text (e.

TextGeometricTransform?

The geometric transformation applied the text.

Public constructors

<init>

SpanStyle(
    color: Color = Color.Unspecified,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontWeight: FontWeight? = null,
    fontStyle: FontStyle? = null,
    fontSynthesis: FontSynthesis? = null,
    fontFamily: FontFamily? = null,
    fontFeatureSettings: String? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    baselineShift: BaselineShift? = null,
    textGeometricTransform: TextGeometricTransform? = null,
    localeList: LocaleList? = null,
    background: Color = Color.Unspecified,
    textDecoration: TextDecoration? = null,
    shadow: Shadow? = null)

Styling configuration for a text span. This configuration only allows character level styling, in order to set paragraph level styling such as line height, or text alignment please see ParagraphStyle.

import androidx.compose.material.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.text.buildAnnotatedString

Text(
    fontSize = 16.sp,
    text = buildAnnotatedString {
        withStyle(style = SpanStyle(color = Color.Red)) {
            append("Hello")
        }
        withStyle(SpanStyle(color = Color.Blue)) {
            append(" World")
        }
    }
)
import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder("Hello")) {
    // push green text style so that any appended text will be green
    pushStyle(SpanStyle(color = Color.Green))
    // append new text, this text will be rendered as green
    append(" World")
    // pop the green style
    pop()
    // append a string without style
    append("!")
    // then style the last added word as red, exclamation mark will be red
    addStyle(SpanStyle(color = Color.Red), "Hello World".length, this.length)

    toAnnotatedString()
}
Parameters
color: Color = Color.Unspecified