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

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


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


```kotlin
inline fun <R : Any> Builder.withStyle(style: SpanStyle, block: Builder.() -> R): R
```


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

#### Parameters

| | |
| --- | --- |
| style | `SpanStyle` to be applied |
| block | function to be executed |


#### Returns

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




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


```kotlin
inline fun <R : Any> Builder.withStyle(
    style: ParagraphStyle,
    crossinline block: Builder.() -> R,
): R
```


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

#### Parameters

| | |
| --- | --- |
| style | `SpanStyle` to be applied |
| block | function to be executed |


#### Returns

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




## Code Examples
### AnnotatedStringBuilderWithStyleSample
```kotlin
fun AnnotatedStringBuilderWithStyleSample() {
    buildAnnotatedString {
        withStyle(SpanStyle(color = Color.Green)) {
            // green text style will be applied to all text in this block
            append("Hello")
        }
        toAnnotatedString()
    }
}
```

