{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

TextUnit

value public final class 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

Nested types

public static class TextUnit.Companion

Public methods

final int

Support comparing Dimensions with comparison operators.

final @NonNull TextUnit
div(double other)

Divide a TextUnit by a scalar.

final @NonNull TextUnit
div(float other)

Divide a TextUnit by a scalar.

final @NonNull TextUnit
div(int other)

Divide a TextUnit by a scalar.

final @NonNull TextUnitType

A type information of this TextUnit.

final float

Returns the value of this TextUnit.

final boolean

True if this is a EM unit type.

final boolean

True if this is a SP unit type.

final @NonNull TextUnit
times(double other)

Multiply a TextUnit by a scalar.

final @NonNull TextUnit
times(float other)

Multiply a TextUnit by a scalar.

final @NonNull TextUnit
times(int other)

Multiply a TextUnit by a scalar.

@NonNull String
final @NonNull TextUnit

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

Extension functions

final boolean

false when this is TextUnit.Unspecified.

final boolean

true when this is TextUnit.Unspecified.

final @NonNull TextUnit
TextUnitKt.takeOrElse(
    @NonNull TextUnit receiver,
    @NonNull Function0<@NonNull TextUnit> block
)

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

Public methods

compareTo

public final int compareTo(@NonNull TextUnit other)

Support comparing Dimensions with comparison operators.

Returns
int

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.

Throws
kotlin.IllegalArgumentException

if this TextUnit and the other has different TextUnitTypes or either of the two has the TextUnitType equals to TextUnitType.Unspecified.

div

public final @NonNull TextUnit div(double other)

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.

div

public final @NonNull TextUnit div(float other)

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.

div

public final @NonNull TextUnit div(int other)

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.

getType

public final @NonNull TextUnitType getType()

A type information of this TextUnit.

getValue

public final float getValue()

Returns the value of this TextUnit.

For example, the value of 3.sp equals to 3, and value of 5.em equals to 5. The value of TextUnits whose TextUnitType is TextUnitType.Unspecified is undefined.

isEm

public final boolean isEm()

True if this is a EM unit type.

isSp

public final boolean isSp()

True if this is a SP unit type.

times

public final @NonNull TextUnit times(double other)

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.

times

public final @NonNull TextUnit times(float other)

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.

times

public final @NonNull TextUnit times(int other)

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.

toString

public @NonNull String toString()

unaryMinus

public final @NonNull TextUnit unaryMinus()

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

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

Extension functions

TextUnitKt.isSpecified

public final boolean TextUnitKt.isSpecified(@NonNull TextUnit receiver)

false when this is TextUnit.Unspecified.

TextUnitKt.isUnspecified

public final boolean TextUnitKt.isUnspecified(@NonNull TextUnit receiver)

true when this is TextUnit.Unspecified.

TextUnitKt.takeOrElse

public final @NonNull TextUnit TextUnitKt.takeOrElse(
    @NonNull TextUnit receiver,
    @NonNull Function0<@NonNull TextUnit> block
)

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