---
title: "withAnnotation"
description: "Pushes an annotation to the [AnnotatedString.Builder], executes [block] and then pops the
annotation."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
inline fun <R : Any> Builder.withAnnotation(
    tag: String,
    annotation: String,
    crossinline block: Builder.() -> R,
): R
```


Pushes an annotation to the `AnnotatedString.Builder`, executes `block` and then pops the
annotation.

#### Parameters

| | |
| --- | --- |
| tag | the tag used to distinguish annotations |
| annotation | the string annotation attached on this AnnotatedString |
| block | function to be executed |


#### Returns

| | |
| --- | --- |
|  | result of the `block` |




<div class='sourceset sourceset-common'>Common</div>


```kotlin
inline fun <R : Any> Builder.withAnnotation(
    ttsAnnotation: TtsAnnotation,
    crossinline block: Builder.() -> R,
): R
```


Pushes an `TtsAnnotation` to the `AnnotatedString.Builder`, executes `block` and then pops the
annotation.

#### Parameters

| | |
| --- | --- |
| ttsAnnotation | an object that stores text to speech metadata that intended for the TTS engine. |
| block | function to be executed |


#### Returns

| | |
| --- | --- |
|  | result of the `block` |




<div class='sourceset sourceset-common'>Common</div>


> **Deprecated** Use LinkAnnotation API for links instead

```kotlin
@ExperimentalTextApi
inline fun <R : Any> Builder.withAnnotation(
    urlAnnotation: UrlAnnotation,
    crossinline block: Builder.() -> R,
): R
```


Pushes an `UrlAnnotation` to the `AnnotatedString.Builder`, executes `block` and then pops the
annotation.

#### Parameters

| | |
| --- | --- |
| urlAnnotation | A `UrlAnnotation` object that stores the URL being linked to. |
| block | function to be executed |


#### Returns

| | |
| --- | --- |
|  | result of the `block` |




