MeasuredText.Builder

public static final class MeasuredText.Builder
extends Object

java.lang.Object
   ↳ android.graphics.text.MeasuredText.Builder


Helper class for creating a MeasuredText.

 
 Paint paint = new Paint();
 String text = "Hello, Android.";
 MeasuredText mt = new MeasuredText.Builder(text.toCharArray())
      .appendStyleRun(paint, text.length, false)
      .build();
 
 

Note: The appendStyle and appendReplacementRun should be called to cover the text length.

Summary

Constants

int HYPHENATION_MODE_FAST

\u3000A value for hyphenation calculation mode.

int HYPHENATION_MODE_NONE

\u3000A value for hyphenation calculation mode.

int HYPHENATION_MODE_NORMAL

\u3000A value for hyphenation calculation mode.

Public constructors

Builder(char[] text)

Construct a builder.

Builder(MeasuredText text)

Construct a builder with existing MeasuredText.

Public methods

MeasuredText.Builder appendReplacementRun(Paint paint, int length, float width)

Used to inform the text layout that the given length is replaced with the object of given width.

MeasuredText.Builder appendStyleRun(Paint paint, LineBreakConfig lineBreakConfig, int length, boolean isRtl)

Apply styles to the given length.

MeasuredText.Builder appendStyleRun(Paint paint, int length, boolean isRtl)

Apply styles to the given length.

MeasuredText build()

Creates a MeasuredText.

MeasuredText.Builder setComputeHyphenation(boolean computeHyphenation)

This method was deprecated in API level 33. use setComputeHyphenation(int) instead.

MeasuredText.Builder setComputeHyphenation(int mode)

By passing true to this method, the build method will calculate hyphenation break points faster with ignoring some typographic features, e.g. kerning.

MeasuredText.Builder setComputeLayout(boolean computeLayout)

By passing true to this method, the build method will compute all full layout information.

Inherited methods

Constants

HYPHENATION_MODE_FAST

Added in API level 33
public static final int HYPHENATION_MODE_FAST

\u3000A value for hyphenation calculation mode. This value indicates that hyphenation points are calculated with faster algorithm. This algorithm measures text width with ignoring the context of hyphen character shaping, e.g. kerning.

Constant Value: 2 (0x00000002)

HYPHENATION_MODE_NONE

Added in API level 33
public static final int HYPHENATION_MODE_NONE

\u3000A value for hyphenation calculation mode. This value indicates that no hyphenation points are calculated.

Constant Value: 0 (0x00000000)

HYPHENATION_MODE_NORMAL

Added in API level 33
public static final int HYPHENATION_MODE_NORMAL

\u3000A value for hyphenation calculation mode. This value indicates that hyphenation points are calculated.

Constant Value: 1 (0x00000001)

Public constructors

Builder

Added in API level 29
public Builder (char[] text)

Construct a builder. The MeasuredText returned by build method will hold a reference of the text. Developer is not supposed to modify the text.

Parameters
text char: a text This value cannot be null.

Builder

Added in API level 29
public Builder (MeasuredText text)

Construct a builder with existing MeasuredText. The MeasuredText returned by build method will hold a reference of the text. Developer is not supposed to modify the text.

Parameters
text MeasuredText: a text This value cannot be null.

Public methods

appendReplacementRun

Added in API level 29
public MeasuredText.Builder appendReplacementRun (Paint paint, 
                int length, 
                float width)

Used to inform the text layout that the given length is replaced with the object of given width. Keeps an internal offset which increases at every append. The initial value for this offset is zero. After the style is applied the internal offset is moved to offset + length, and next call will start from this new position. Informs the layout engine that the given length should not be processed, instead the provided width should be used for calculating the width of that range.

Parameters
paint Paint: This value cannot be null.

length int: a length to be replaced with the object, can not exceed the length of the text Value is 0 or greater

width float: a replacement width of the range This units of this value are pixels. Value is 0 or greater

Returns
MeasuredText.Builder This value cannot be null.

appendStyleRun

Added in API level 33
public MeasuredText.Builder appendStyleRun (Paint paint, 
                LineBreakConfig lineBreakConfig, 
                int length, 
                boolean isRtl)

Apply styles to the given length. Keeps an internal offset which increases at every append. The initial value for this offset is zero. After the style is applied the internal offset is moved to offset + length, and next call will start from this new position.

Parameters
paint Paint: a paint This value cannot be null.

lineBreakConfig LineBreakConfig: a line break configuration. This value may be null.

length int: a length to be applied with a given paint, can not exceed the length of the text Value is 0 or greater

isRtl boolean: true if the text is in RTL context, otherwise false.

Returns
MeasuredText.Builder This value cannot be null.

appendStyleRun

Added in API level 29
public MeasuredText.Builder appendStyleRun (Paint paint, 
                int length, 
                boolean isRtl)

Apply styles to the given length. Keeps an internal offset which increases at every append. The initial value for this offset is zero. After the style is applied the internal offset is moved to offset + length, and next call will start from this new position.

ERROR(Paint#TEXT_RUN_FLAG_RIGHT_EDGE/android.graphics.Paint#TEXT_RUN_FLAG_RIGHT_EDGE Paint#TEXT_RUN_FLAG_RIGHT_EDGE) and ERROR(Paint#TEXT_RUN_FLAG_LEFT_EDGE/android.graphics.Paint#TEXT_RUN_FLAG_LEFT_EDGE Paint#TEXT_RUN_FLAG_LEFT_EDGE) are ignored and treated as both of them are set.

Parameters
paint Paint: a paint This value cannot be null.

length int: a length to be applied with a given paint, can not exceed the length of the text Value is 0 or greater

isRtl boolean: true if the text is in RTL context, otherwise false.

Returns
MeasuredText.Builder This value cannot be null.

build

Added in API level 29
public MeasuredText build ()

Creates a MeasuredText. Once you called build() method, you can't reuse the Builder class again.

Returns
MeasuredText This value cannot be null.

Throws
IllegalStateException if this Builder is reused.
IllegalStateException if the whole text is not covered by one or more runs (style or replacement)

setComputeHyphenation

Added in API level 29
Deprecated in API level 33
public MeasuredText.Builder setComputeHyphenation (boolean computeHyphenation)

This method was deprecated in API level 33.
use setComputeHyphenation(int) instead.

By passing true to this method, the build method will compute all possible hyphenation pieces as well. If you don't want to use automatic hyphenation, you can pass false to this method and save the computation time of hyphenation. The default value is false. Even if you pass false to this method, you can still enable automatic hyphenation of LineBreaker but line break computation becomes slower.

Parameters
computeHyphenation boolean: true if you want to use automatic hyphenations.

Returns
MeasuredText.Builder This value cannot be null.

setComputeHyphenation

Added in API level 33
public MeasuredText.Builder setComputeHyphenation (int mode)

By passing true to this method, the build method will calculate hyphenation break points faster with ignoring some typographic features, e.g. kerning. HYPHENATION_MODE_NONE is by default.

Parameters
mode int: a hyphenation mode. Value is HYPHENATION_MODE_NONE, HYPHENATION_MODE_NORMAL, or HYPHENATION_MODE_FAST

Returns
MeasuredText.Builder This value cannot be null.

setComputeLayout

Added in API level 29
public MeasuredText.Builder setComputeLayout (boolean computeLayout)

By passing true to this method, the build method will compute all full layout information. If you don't use MeasuredText#getBounds(int,int,android.graphics.Rect), you can pass false to this method and save the memory spaces. The default value is true. Even if you pass false to this method, you can still call getBounds but it becomes slower.

Parameters
computeLayout boolean: true if you want to retrieve full layout info, e.g. bbox.

Returns
MeasuredText.Builder This value cannot be null.