SpanStyle
-
Cmn
class SpanStyle : AnnotatedString.Annotation
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.graphics.Color import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.sp 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.graphics.Color import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString buildAnnotatedString { append("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() }
| See also | |
|---|---|
AnnotatedString |
|
TextStyle |
|
ParagraphStyle |
Summary
Public constructors |
|
|---|---|
SpanStyle(Styling configuration for a text span. |
Cmn
|
SpanStyle(Styling configuration for a text span. |
Cmn
|
Public functions |
||
|---|---|---|
SpanStyle |
copy( |
Cmn
|
SpanStyle |
copy( |
Cmn
|
open operator Boolean |
Cmn
|
|
open Int |
hashCode() |
Cmn
|
SpanStyle |
Returns a new span style that is a combination of this style and the given |
Cmn
|
operator SpanStyle |
Plus operator overload that applies a |
Cmn
|
open String |
toString() |
Cmn
|
Public properties |
||
|---|---|---|
Float |
Opacity of text. |
Cmn
|
Color |
The background color for the text. |
Cmn
|
BaselineShift? |
The amount by which the text is shifted up from the current baseline. |
Cmn
|
Brush? |
Brush to draw text. |
Cmn
|
Color |
Color to draw text. |
Cmn
|
DrawStyle? |
Drawing style of text, whether fill in the text while drawing or stroke around the edges. |
Cmn
|
FontFamily? |
The font family to be used when rendering the text. |
Cmn
|
String? |
The advanced typography settings provided by font. |
Cmn
|
TextUnit |
The size of glyphs (in logical pixels) to use when painting the text. |
Cmn
|
FontStyle? |
The typeface variant to use when drawing the letters (e.g., italic). |
Cmn
|
FontSynthesis? |
Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family. |
Cmn
|
FontWeight? |
The typeface thickness to use when painting the text (e.g., bold). |
Cmn
|
TextUnit |
The amount of space (in em) to add between each letter. |
Cmn
|
LocaleList? |
The locale list used to select region-specific glyphs. |
Cmn
|
PlatformSpanStyle? |
Platform specific |
Cmn
|
Shadow? |
The shadow effect applied on the text. |
Cmn
|
TextDecoration? |
The decorations to paint on the text (e.g., an underline). |
Cmn
|
TextGeometricTransform? |
The geometric transformation applied the text. |
Cmn
|
Public constructors
SpanStyle
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,
platformStyle: PlatformSpanStyle? = null,
drawStyle: DrawStyle? = 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.graphics.Color import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.sp 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.graphics.Color import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString buildAnnotatedString { append("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 |
The color to draw the text. |
fontSize: TextUnit = TextUnit.Unspecified |
The size of glyphs (in logical pixels) to use when painting the text. This may be |
fontWeight: FontWeight? = null |
The typeface thickness to use when painting the text (e.g., bold). |
fontStyle: FontStyle? = null |
The typeface variant to use when drawing the letters (e.g., italic). |
fontSynthesis: FontSynthesis? = null |
Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family. |
fontFamily: FontFamily? = null |
The font family to be used when rendering the text. |
fontFeatureSettings: String? = null |
The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop |
letterSpacing: TextUnit = TextUnit.Unspecified |
The amount of space (in em) to add between each letter. |
baselineShift: BaselineShift? = null |
The amount by which the text is shifted up from the current baseline. |
textGeometricTransform: TextGeometricTransform? = null |
The geometric transformation applied the text. |
localeList: LocaleList? = null |
The locale list used to select region-specific glyphs. |
background: Color = Color.Unspecified |
The background color for the text. |
textDecoration: TextDecoration? = null |
The decorations to paint on the text (e.g., an underline). |
shadow: Shadow? = null |
The shadow effect applied on the text. |
platformStyle: PlatformSpanStyle? = null |
Platform specific |
drawStyle: DrawStyle? = null |
Drawing style of text, whether fill in the text while drawing or stroke around the edges. |
| See also | |
|---|---|
AnnotatedString |
|
TextStyle |
|
ParagraphStyle |
SpanStyle
SpanStyle(
brush: Brush?,
alpha: Float = Float.NaN,
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,
platformStyle: PlatformSpanStyle? = null,
drawStyle: DrawStyle? = 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.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.sp val brushColors = listOf(Color.Red, Color.Blue, Color.Green, Color.Yellow) Text( fontSize = 16.sp, text = buildAnnotatedString { withStyle(SpanStyle(brush = Brush.radialGradient(brushColors))) { append("Hello") } withStyle(SpanStyle(brush = Brush.radialGradient(brushColors.asReversed()))) { append(" World") } }, )
import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString buildAnnotatedString { append("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 | |
|---|---|
brush: Brush? |
The brush to use when painting the text. If brush is given as null, it will be treated as unspecified. It is equivalent to calling the alternative color constructor with |
alpha: Float = Float.NaN |
Opacity to be applied to |
fontSize: TextUnit = TextUnit.Unspecified |
The size of glyphs (in logical pixels) to use when painting the text. This may be |
fontWeight: FontWeight? = null |
The typeface thickness to use when painting the text (e.g., bold). |
fontStyle: FontStyle? = null |
The typeface variant to use when drawing the letters (e.g., italic). |
fontSynthesis: FontSynthesis? = null |
Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family. |
fontFamily: FontFamily? = null |
The font family to be used when rendering the text. |
fontFeatureSettings: String? = null |
The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop |
letterSpacing: TextUnit = TextUnit.Unspecified |
The amount of space (in em) to add between each letter. |
baselineShift: BaselineShift? = null |
The amount by which the text is shifted up from the current baseline. |
textGeometricTransform: TextGeometricTransform? = null |
The geometric transformation applied the text. |
localeList: LocaleList? = null |
The locale list used to select region-specific glyphs. |
background: Color = Color.Unspecified |
The background color for the text. |
textDecoration: TextDecoration? = null |
The decorations to paint on the text (e.g., an underline). |
shadow: Shadow? = null |
The shadow effect applied on the text. |
platformStyle: PlatformSpanStyle? = null |
Platform specific |
drawStyle: DrawStyle? = null |
Drawing style of text, whether fill in the text while drawing or stroke around the edges. |
| See also | |
|---|---|
AnnotatedString |
|
TextStyle |
|
ParagraphStyle |
Public functions
copy
fun copy(
color: Color = this.color,
fontSize: TextUnit = this.fontSize,
fontWeight: FontWeight? = this.fontWeight,
fontStyle: FontStyle? = this.fontStyle,
fontSynthesis: FontSynthesis? = this.fontSynthesis,
fontFamily: FontFamily? = this.fontFamily,
fontFeatureSettings: String? = this.fontFeatureSettings,
letterSpacing: TextUnit = this.letterSpacing,
baselineShift: BaselineShift? = this.baselineShift,
textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform,
localeList: LocaleList? = this.localeList,
background: Color = this.background,
textDecoration: TextDecoration? = this.textDecoration,
shadow: Shadow? = this.shadow,
platformStyle: PlatformSpanStyle? = this.platformStyle,
drawStyle: DrawStyle? = this.drawStyle
): SpanStyle
copy
fun copy(
brush: Brush?,
alpha: Float = this.alpha,
fontSize: TextUnit = this.fontSize,
fontWeight: FontWeight? = this.fontWeight,
fontStyle: FontStyle? = this.fontStyle,
fontSynthesis: FontSynthesis? = this.fontSynthesis,
fontFamily: FontFamily? = this.fontFamily,
fontFeatureSettings: String? = this.fontFeatureSettings,
letterSpacing: TextUnit = this.letterSpacing,
baselineShift: BaselineShift? = this.baselineShift,
textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform,
localeList: LocaleList? = this.localeList,
background: Color = this.background,
textDecoration: TextDecoration? = this.textDecoration,
shadow: Shadow? = this.shadow,
platformStyle: PlatformSpanStyle? = this.platformStyle,
drawStyle: DrawStyle? = this.drawStyle
): SpanStyle
merge
fun merge(other: SpanStyle? = null): SpanStyle
Returns a new span style that is a combination of this style and the given other style.
other span style's null or inherit properties are replaced with the non-null properties of this span style. Another way to think of it is that the "missing" properties of the other style are filled by the properties of this style.
If the given span style is null, returns this span style.
Public properties
alpha
val alpha: Float
Opacity of text. This value is either provided along side Brush, or via alpha channel in color.
baselineShift
val baselineShift: BaselineShift?
The amount by which the text is shifted up from the current baseline.
drawStyle
val drawStyle: DrawStyle?
Drawing style of text, whether fill in the text while drawing or stroke around the edges.
fontFeatureSettings
val fontFeatureSettings: String?
The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop
fontSize
val fontSize: TextUnit
The size of glyphs (in logical pixels) to use when painting the text. This may be TextUnit.Unspecified for inheriting from another SpanStyle.
fontStyle
val fontStyle: FontStyle?
The typeface variant to use when drawing the letters (e.g., italic).
fontSynthesis
val fontSynthesis: FontSynthesis?
Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family.
fontWeight
val fontWeight: FontWeight?
The typeface thickness to use when painting the text (e.g., bold).
textDecoration
val textDecoration: TextDecoration?
The decorations to paint on the text (e.g., an underline).
textGeometricTransform
val textGeometricTransform: TextGeometricTransform?
The geometric transformation applied the text.