TextStyle
-
Cmn
class TextStyle
Styling configuration for a Text.
import androidx.compose.material.Text import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp Text( text = "Demo Text", style = TextStyle( color = Color.Red, fontSize = 16.sp, fontFamily = FontFamily.Monospace, fontWeight = FontWeight.W800, fontStyle = FontStyle.Italic, letterSpacing = 0.5.em, background = Color.LightGray, textDecoration = TextDecoration.Underline, ), )
| See also | |
|---|---|
AnnotatedString |
|
SpanStyle |
|
ParagraphStyle |
Summary
Public constructors |
|
|---|---|
TextStyle(Styling configuration for a |
Cmn
|
TextStyle(Styling configuration for a |
Cmn
|
Public functions |
||
|---|---|---|
TextStyle |
copy( |
Cmn
|
TextStyle |
copy( |
Cmn
|
open operator Boolean |
Cmn
|
|
Boolean |
Cmn
|
|
Boolean |
Returns true if text layout affecting attributes between this TextStyle and other are the same. |
Cmn
|
open Int |
hashCode() |
Cmn
|
TextStyle |
merge(other: ParagraphStyle)Returns a new text style that is a combination of this style and the given |
Cmn
|
TextStyle |
Returns a new text style that is a combination of this style and the given |
Cmn
|
TextStyle |
Returns a new text style that is a combination of this style and the given |
Cmn
|
TextStyle |
merge(Fast merge non-default values and parameters. |
Cmn
|
operator TextStyle |
plus(other: ParagraphStyle)Plus operator overload that applies a |
Cmn
|
operator TextStyle |
Plus operator overload that applies a |
Cmn
|
operator TextStyle |
Plus operator overload that applies a |
Cmn
|
ParagraphStyle |
Cmn
|
|
SpanStyle |
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? |
The brush to use when drawing text. |
Cmn
|
Color |
The text color. |
Cmn
|
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
|
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 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
|
Hyphens |
The hyphens configuration of the paragraph. |
Cmn
|
TextUnit |
The amount of space to add between each letter. |
Cmn
|
LineBreak |
The line breaking configuration of the paragraph. |
Cmn
|
TextUnit |
Line height for the |
Cmn
|
LineHeightStyle? |
The configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line. |
Cmn
|
LocaleList? |
The locale list used to select region-specific glyphs. |
Cmn
|
PlatformTextStyle? |
Platform specific |
Cmn
|
Shadow? |
The shadow effect applied on the text. |
Cmn
|
TextAlign |
The alignment of the text within the lines of the paragraph. |
Cmn
|
TextDecoration? |
The decorations to paint on the text (e.g., an underline). |
Cmn
|
TextDirection |
The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. |
Cmn
|
TextGeometricTransform? |
The geometric transformation applied the text. |
Cmn
|
TextIndent? |
The indentation of the paragraph. |
Cmn
|
TextMotion? |
Text character placement configuration, whether to optimize for animated or static text. |
Cmn
|
Public constructors
TextStyle
TextStyle(
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,
drawStyle: DrawStyle? = null,
textAlign: TextAlign = TextAlign.Unspecified,
textDirection: TextDirection = TextDirection.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
textIndent: TextIndent? = null,
platformStyle: PlatformTextStyle? = null,
lineHeightStyle: LineHeightStyle? = null,
lineBreak: LineBreak = LineBreak.Unspecified,
hyphens: Hyphens = Hyphens.Unspecified,
textMotion: TextMotion? = null
)
Styling configuration for a Text.
import androidx.compose.material.Text import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp Text( text = "Demo Text", style = TextStyle( color = Color.Red, fontSize = 16.sp, fontFamily = FontFamily.Monospace, fontWeight = FontWeight.W800, fontStyle = FontStyle.Italic, letterSpacing = 0.5.em, background = Color.LightGray, textDecoration = TextDecoration.Underline, ), )
| Parameters | |
|---|---|
color: Color = Color.Unspecified |
The text color. |
fontSize: TextUnit = TextUnit.Unspecified |
The size of glyphs 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 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. |
drawStyle: DrawStyle? = null |
Drawing style of text, whether fill in the text while drawing or stroke around the edges. |
textAlign: TextAlign = TextAlign.Unspecified |
The alignment of the text within the lines of the paragraph. |
textDirection: TextDirection = TextDirection.Unspecified |
The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the |
lineHeight: TextUnit = TextUnit.Unspecified |
Line height for the |
textIndent: TextIndent? = null |
The indentation of the paragraph. |
platformStyle: PlatformTextStyle? = null |
Platform specific |
lineHeightStyle: LineHeightStyle? = null |
the configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line. The configuration is applied only when a |
lineBreak: LineBreak = LineBreak.Unspecified |
The line breaking configuration for the text. |
hyphens: Hyphens = Hyphens.Unspecified |
The configuration of hyphenation. |
textMotion: TextMotion? = null |
Text character placement, whether to optimize for animated or static text. |
TextStyle
TextStyle(
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,
drawStyle: DrawStyle? = null,
textAlign: TextAlign = TextAlign.Unspecified,
textDirection: TextDirection = TextDirection.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
textIndent: TextIndent? = null,
platformStyle: PlatformTextStyle? = null,
lineHeightStyle: LineHeightStyle? = null,
lineBreak: LineBreak = LineBreak.Unspecified,
hyphens: Hyphens = Hyphens.Unspecified,
textMotion: TextMotion? = null
)
Styling configuration for a Text.
import androidx.compose.material.Text import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp Text( text = "Demo Text", style = TextStyle( brush = Brush.linearGradient(listOf(Color.Red, Color.Blue, Color.Green)), alpha = 0.8f, fontSize = 16.sp, fontFamily = FontFamily.Monospace, fontWeight = FontWeight.W800, fontStyle = FontStyle.Italic, letterSpacing = 0.5.em, textDecoration = TextDecoration.Underline, ), )
| 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 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 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. |
drawStyle: DrawStyle? = null |
Drawing style of text, whether fill in the text while drawing or stroke around the edges. |
textAlign: TextAlign = TextAlign.Unspecified |
The alignment of the text within the lines of the paragraph. |
textDirection: TextDirection = TextDirection.Unspecified |
The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the |
lineHeight: TextUnit = TextUnit.Unspecified |
Line height for the |
textIndent: TextIndent? = null |
The indentation of the paragraph. |
platformStyle: PlatformTextStyle? = null |
Platform specific |
lineHeightStyle: LineHeightStyle? = null |
the configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line. The configuration is applied only when a |
lineBreak: LineBreak = LineBreak.Unspecified |
The line breaking configuration for the text. |
hyphens: Hyphens = Hyphens.Unspecified |
The configuration of hyphenation. |
textMotion: TextMotion? = null |
Text character placement, whether to optimize for animated or static text. |
Public functions
copy
fun copy(
color: Color = this.spanStyle.color,
fontSize: TextUnit = this.spanStyle.fontSize,
fontWeight: FontWeight? = this.spanStyle.fontWeight,
fontStyle: FontStyle? = this.spanStyle.fontStyle,
fontSynthesis: FontSynthesis? = this.spanStyle.fontSynthesis,
fontFamily: FontFamily? = this.spanStyle.fontFamily,
fontFeatureSettings: String? = this.spanStyle.fontFeatureSettings,
letterSpacing: TextUnit = this.spanStyle.letterSpacing,
baselineShift: BaselineShift? = this.spanStyle.baselineShift,
textGeometricTransform: TextGeometricTransform? = this.spanStyle.textGeometricTransform,
localeList: LocaleList? = this.spanStyle.localeList,
background: Color = this.spanStyle.background,
textDecoration: TextDecoration? = this.spanStyle.textDecoration,
shadow: Shadow? = this.spanStyle.shadow,
drawStyle: DrawStyle? = this.spanStyle.drawStyle,
textAlign: TextAlign = this.paragraphStyle.textAlign,
textDirection: TextDirection = this.paragraphStyle.textDirection,
lineHeight: TextUnit = this.paragraphStyle.lineHeight,
textIndent: TextIndent? = this.paragraphStyle.textIndent,
platformStyle: PlatformTextStyle? = this.platformStyle,
lineHeightStyle: LineHeightStyle? = this.paragraphStyle.lineHeightStyle,
lineBreak: LineBreak = this.paragraphStyle.lineBreak,
hyphens: Hyphens = this.paragraphStyle.hyphens,
textMotion: TextMotion? = this.paragraphStyle.textMotion
): TextStyle
copy
fun copy(
brush: Brush?,
alpha: Float = this.spanStyle.alpha,
fontSize: TextUnit = this.spanStyle.fontSize,
fontWeight: FontWeight? = this.spanStyle.fontWeight,
fontStyle: FontStyle? = this.spanStyle.fontStyle,
fontSynthesis: FontSynthesis? = this.spanStyle.fontSynthesis,
fontFamily: FontFamily? = this.spanStyle.fontFamily,
fontFeatureSettings: String? = this.spanStyle.fontFeatureSettings,
letterSpacing: TextUnit = this.spanStyle.letterSpacing,
baselineShift: BaselineShift? = this.spanStyle.baselineShift,
textGeometricTransform: TextGeometricTransform? = this.spanStyle.textGeometricTransform,
localeList: LocaleList? = this.spanStyle.localeList,
background: Color = this.spanStyle.background,
textDecoration: TextDecoration? = this.spanStyle.textDecoration,
shadow: Shadow? = this.spanStyle.shadow,
drawStyle: DrawStyle? = this.spanStyle.drawStyle,
textAlign: TextAlign = this.paragraphStyle.textAlign,
textDirection: TextDirection = this.paragraphStyle.textDirection,
lineHeight: TextUnit = this.paragraphStyle.lineHeight,
textIndent: TextIndent? = this.paragraphStyle.textIndent,
platformStyle: PlatformTextStyle? = this.platformStyle,
lineHeightStyle: LineHeightStyle? = this.paragraphStyle.lineHeightStyle,
lineBreak: LineBreak = this.paragraphStyle.lineBreak,
hyphens: Hyphens = this.paragraphStyle.hyphens,
textMotion: TextMotion? = this.paragraphStyle.textMotion
): TextStyle
hasSameLayoutAffectingAttributes
fun hasSameLayoutAffectingAttributes(other: TextStyle): Boolean
Returns true if text layout affecting attributes between this TextStyle and other are the same.
The attributes that do not require a layout change are color, textDecoration and shadow.
Majority of attributes change text layout, and examples are line height, font properties, font size, locale etc.
This function can be used to identify if a new text layout is required for a given TextStyle.
| Parameters | |
|---|---|
other: TextStyle |
The TextStyle to compare to. |
merge
fun merge(other: ParagraphStyle): TextStyle
Returns a new text style that is a combination of this style and the given other style.
| See also | |
|---|---|
merge |
merge
fun merge(other: SpanStyle): TextStyle
Returns a new text style that is a combination of this style and the given other style.
| See also | |
|---|---|
merge |
merge
fun merge(other: TextStyle? = null): TextStyle
Returns a new text style that is a combination of this style and the given other style.
other text style's null or inherit properties are replaced with the non-null properties of this text 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 text style is null, returns this text style.
merge
fun merge(
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,
drawStyle: DrawStyle? = null,
textAlign: TextAlign = TextAlign.Unspecified,
textDirection: TextDirection = TextDirection.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
textIndent: TextIndent? = null,
lineHeightStyle: LineHeightStyle? = null,
lineBreak: LineBreak = LineBreak.Unspecified,
hyphens: Hyphens = Hyphens.Unspecified,
platformStyle: PlatformTextStyle? = null,
textMotion: TextMotion? = null
): TextStyle
Fast merge non-default values and parameters.
This is the same algorithm as merge but does not require allocating it's parameter and may return this instead of allocating a result when all values are default.
This is a similar algorithm to copy but when either this or a parameter are set to a default value, the other value will take precedent.
To explain better, consider the following examples:
Example 1:
-
this.color =
Color.Unspecified -
result =>
Color.Red
Example 2:
Example 3:
-
this.color =
Color.Red -
result =>
Color.Blue
You should always use this method over the merge overload when you do not already have a TextStyle allocated. You should chose this over copy when building a theming system and applying styling information to a specific usage.
| Returns | |
|---|---|
TextStyle |
this or a new TextLayoutResult with all parameters chosen to the non-default option provided. |
| See also | |
|---|---|
merge |
plus
operator fun plus(other: ParagraphStyle): TextStyle
Plus operator overload that applies a merge.
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 to use when painting the text. This may be TextUnit.Unspecified for inheriting from another TextStyle.
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).
lineHeightStyle
val lineHeightStyle: LineHeightStyle?
The configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line.
The configuration is applied only when a lineHeight is defined.
When null, LineHeightStyle.Default is used.
textDecoration
val textDecoration: TextDecoration?
The decorations to paint on the text (e.g., an underline).
textDirection
val textDirection: TextDirection
The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the LayoutDirection as the primary signal.
textGeometricTransform
val textGeometricTransform: TextGeometricTransform?
The geometric transformation applied the text.
textMotion
val textMotion: TextMotion?
Text character placement configuration, whether to optimize for animated or static text.