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

TextUnit

@Immutable inline class TextUnit
kotlin.Any
   ↳ androidx.compose.ui.unit.TextUnit

The unit used for text related dimension value.

This unit can hold either scaled pixels (SP), relative font size (EM) and special unit Unspecified for indicating inheriting from other style or using the default value. It can be created with sp or em. (e.g. 15.sp or 18.em) which can be applied to Int, Double, and Float.

Note that do not store this value in your persistent storage or send to another process since the internal representation may be changed in future.

Summary

Public methods
operator Int

Support comparing Dimensions with comparison operators.

operator TextUnit
div(other: Float)

Divide a TextUnit by a scalar.

operator TextUnit
div(other: Double)

Divide a TextUnit by a scalar.

operator TextUnit
div(other: Int)

Divide a TextUnit by a scalar.

operator TextUnit
times(other: Float)

Multiply a TextUnit by a scalar.

operator TextUnit
times(other: Double)

Multiply a TextUnit by a scalar.

operator TextUnit
times(other: Int)

Multiply a TextUnit by a scalar.

String

operator TextUnit

This is the same as multiplying the TextUnit by -1.

Extension functions
From androidx.compose.ui.unit
TextUnit
TextUnit.takeOrElse(block: () -> TextUnit)

If this TextUnit then this is returned, otherwise block is executed and its result is returned.

Properties
Boolean

True if this is a EM unit type.

Boolean

True if this is a SP unit type.

TextUnitType

A type information of this TextUnit.

Float

Returns the value of this TextUnit.

Companion properties
TextUnit

A special TextUnit instance for representing inheriting from parent value.

Extension properties
From androidx.compose.ui.unit
Boolean

false when this is TextUnit.Unspecified.

Boolean

true when this is TextUnit.Unspecified.

Public methods

compareTo

inline operator fun compareTo(other: TextUnit): Int

Support comparing Dimensions with comparison operators.

Return
0 if this TextUnit equals to the other, a negative number if it's less than the other, or a positive number if it's greater than the other.
Exceptions
IllegalArgumentException if this TextUnit and the other has different TextUnitTypes or either of the two has the TextUnitType equals to TextUnitType.Unspecified.

div

inline operator fun div(other: Float): TextUnit

Divide a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is TextUnitType.Unspecified.

div

inline operator fun div(other: Double): TextUnit

Divide a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is TextUnitType.Unspecified.

div

inline operator fun div(other: Int): TextUnit

Divide a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is TextUnitType.Unspecified.

times

inline operator fun times(other: Float): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is TextUnitType.Unspecified.

times

inline operator fun times(other: Double): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is TextUnitType.Unspecified.

times

inline operator fun times(other: Int): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the left operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

Exceptions
IllegalArgumentException if this TextUnit's type is T