Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Builder

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/android/graphics/text/MeasuredText.Builder "View this page in Java")

```
class Builder
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [android.graphics.text.MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) |

Helper class for creating a `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText`.

```kotlin
<code>
  Paint paint = new Paint();
  String text = "Hello, Android.";
  MeasuredText mt = new MeasuredText.Builder(text.toCharArray())
       .appendStyleRun(paint, text.length, false)
       .build();
  </code>
```

<br />

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

## Summary

| Constants ||
|---|---|
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_FAST:kotlin.Int` A value for hyphenation calculation mode. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_NONE:kotlin.Int` A value for hyphenation calculation mode. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_NORMAL:kotlin.Int` A value for hyphenation calculation mode. |

| Public constructors ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#Builder(android.graphics.text.MeasuredText)(text: https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText)` Construct a builder with existing MeasuredText. |
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#Builder(kotlin.CharArray)(text: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)` Construct a builder. |

| Public methods ||
|---|---|
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#appendReplacementRun(android.graphics.Paint,%20kotlin.Int,%20kotlin.Float)(paint: https://developer.android.com/reference/kotlin/android/graphics/Paint.html#, length: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, width: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html)` Used to inform the text layout that the given length is replaced with the object of given width. |
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#appendStyleRun(android.graphics.Paint,%20android.graphics.text.LineBreakConfig,%20kotlin.Int,%20kotlin.Boolean)(paint: https://developer.android.com/reference/kotlin/android/graphics/Paint.html#, lineBreakConfig: https://developer.android.com/reference/kotlin/android/graphics/text/LineBreakConfig?, length: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, isRtl: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` Apply styles to the given length. |
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#appendStyleRun(android.graphics.Paint,%20kotlin.Int,%20kotlin.Boolean)(paint: https://developer.android.com/reference/kotlin/android/graphics/Paint.html#, length: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, isRtl: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` Apply styles to the given length. |
| [MeasuredText](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#build()()` Creates a MeasuredText. |
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#setComputeHyphenation(kotlin.Boolean)(computeHyphenation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` By passing true to this method, the build method will compute all possible hyphenation pieces as well. |
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#setComputeHyphenation(kotlin.Int)(mode: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` By passing true to this method, the build method will calculate hyphenation break points faster with ignoring some typographic features, e. |
| [MeasuredText.Builder](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#) | `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#setComputeLayout(kotlin.Boolean)(computeLayout: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)` By passing true to this method, the build method will compute all full layout information. |

## Constants

### HYPHENATION_MODE_FAST

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
static val HYPHENATION_MODE_FAST: Int
```

A 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.

    Value: 2

### HYPHENATION_MODE_NONE

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
static val HYPHENATION_MODE_NONE: Int
```

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

    Value: 0

### HYPHENATION_MODE_NORMAL

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
static val HYPHENATION_MODE_NORMAL: Int
```

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

    Value: 1

## Public constructors

### Builder

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
Builder(text: MeasuredText)
```

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](https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText): a text. This value cannot be `null`. |

### Builder

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
Builder(text: CharArray)
```

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` | [CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html): a text. This value cannot be `null`. |

## Public methods

### appendReplacementRun

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun appendReplacementRun(
    paint: Paint, 
    length: Int, 
    width: Float
): MeasuredText.Builder
```

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](https://developer.android.com/reference/kotlin/android/graphics/Paint.html#): This value cannot be `null`. |
| `length` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): a length to be replaced with the object, can not exceed the length of the text. Value is 0 or greater |
| `width` | [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html): a replacement width of the range. The units of this value are pixels. Value is 0.0f or greater |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |

### appendStyleRun

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun appendStyleRun(
    paint: Paint, 
    lineBreakConfig: LineBreakConfig?, 
    length: Int, 
    isRtl: Boolean
): MeasuredText.Builder
```

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](https://developer.android.com/reference/kotlin/android/graphics/Paint.html#): a paint. This value cannot be `null`. |
| `lineBreakConfig` | [LineBreakConfig](https://developer.android.com/reference/kotlin/android/graphics/text/LineBreakConfig)?: a line break configuration. This value may be `null`. |
| `length` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): a length to be applied with a given paint, can not exceed the length of the text. Value is 0 or greater |
| `isRtl` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): true if the text is in RTL context, otherwise false. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |

### appendStyleRun

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun appendStyleRun(
    paint: Paint, 
    length: Int, 
    isRtl: Boolean
): MeasuredText.Builder
```

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.

`https://developer.android.com/reference/kotlin/android/graphics/Paint.html#TEXT_RUN_FLAG_RIGHT_EDGE:kotlin.Int` and `https://developer.android.com/reference/kotlin/android/graphics/Paint.html#TEXT_RUN_FLAG_LEFT_EDGE:kotlin.Int` are ignored and treated as both of them are set.

| Parameters ||
|---|---|
| `paint` | [Paint](https://developer.android.com/reference/kotlin/android/graphics/Paint.html#): a paint. This value cannot be `null`. |
| `length` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): a length to be applied with a given paint, can not exceed the length of the text. Value is 0 or greater |
| `isRtl` | [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): true if the text is in RTL context, otherwise false. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |

### build

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun build(): MeasuredText
```

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

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText` | This value cannot be `null`. |

| Exceptions ||
|---|---|
| `java.lang.IllegalStateException` | if the whole text is not covered by one or more runs (style or replacement) |

### setComputeHyphenation

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  
Deprecated in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun setComputeHyphenation(computeHyphenation: Boolean): MeasuredText.Builder
```

**Deprecated:** *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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): true if you want to use automatic hyphenations. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |

### setComputeHyphenation

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun setComputeHyphenation(mode: Int): MeasuredText.Builder
```

By passing true to this method, the build method will calculate hyphenation break points faster with ignoring some typographic features, e.g. kerning. `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_NONE:kotlin.Int` is by default.

| Parameters ||
|---|---|
| `mode` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): a hyphenation mode. Value is one of the following: - `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_NONE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_NORMAL:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#HYPHENATION_MODE_FAST:kotlin.Int` |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |

### setComputeLayout

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun setComputeLayout(computeLayout: Boolean): MeasuredText.Builder
```

By passing true to this method, the build method will compute all full layout information. If you don't use `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText#getBounds(kotlin.Int,%20kotlin.Int,%20android.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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html): true if you want to retrieve full layout info, e.g. bbox. |

| Return ||
|---|---|
| `https://developer.android.com/reference/kotlin/android/graphics/text/MeasuredText.Builder#` | This value cannot be `null`. |