# TextHelper

`com.jsmacrosce.jsmacros.client.api.helper.TextHelper`

**Since:** 1.0.8

Wraps a Minecraft text component, which can contain text with different styles and click or
hover behavior.  

[visit(com.jsmacrosce.jsmacros.core.MethodWrapper)](#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_visit-MethodWrapper-) visits each text run with its
[StyleHelper](StyleHelper.md); [getString()](#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getString-) returns the flattened text without styling.  

Create helpers from strings, translation keys, or component JSON with the corresponding
`Chat.createTextHelperFrom...` methods. [replaceFromString(String)](#) replaces this
helper's component; it does not update text already displayed elsewhere.  

```js
// Read the component's flattened text
const line = Chat.createTextHelperFromString("some styled line");  
Chat.log(`flattened: ${line.getString()}`);  

// Inspect each formatted run
line.visit(JavaWrapper.methodToJava(function (style, run) {
  if (style.bold()) {
    Chat.log(`bold run: ${run}`);  
  }
}));  

// Build translated text for display
Chat.actionbar(Chat.createTextHelperFromTranslationKey("block.minecraft.stone"));  
```

## Fields

### STRIP_FORMATTING_PATTERN {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_STRIP_FORMATTING_PATTERN}

```js
static STRIP_FORMATTING_PATTERN: Pattern
```

Matches a section sign followed by a legacy formatting code (`0-9`, `A-F`,  
`K-O`, or `R`), case-insensitively. Used by
[getStringStripFormatting()](#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getStringStripFormatting-) and [withoutFormatting()](#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_withoutFormatting-).

**Since:** 1.0.8

## Methods

### getJson {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getJson-}

```js
getJson(): String
```

**Returns:** the component serialized as JSON using the current player's registry access

**Since:** 1.2.7

### getString {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getString-}

```js
getString(): String
```

**Returns:** the component's flattened text, without style information

**Since:** 1.2.7

### getStringStripFormatting {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getStringStripFormatting-}

```js
getStringStripFormatting(): String
```

**Returns:** the flattened text with legacy section-sign formatting codes removed

**Since:** 1.6.5

### getWidth {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getWidth-}

```js
getWidth(): int
```

**Returns:** the rendered width of the component in client font units

**Since:** 1.8.4

### toString {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_toString-}

```js
toString(): String
```

**Returns:** a `TextHelper:{"text": "..."}` representation containing the flattened text

**Since:** 1.0.8

**Note:** this used to do the same as [getString](#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_getString-)

### visit {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_visit-MethodWrapper-}

```js
visit(MethodWrapper<StyleHelper, String, Object, ?> visitor): TextHelper
```

Visits each text run with its style and text, then returns this helper.

**Parameters:**
- `visitor`: callback receiving a [StyleHelper](StyleHelper.md) and the run's text

**Returns:** self for chaining

**Since:** 1.6.5

### withoutFormatting {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_withoutFormatting-}

```js
withoutFormatting(): TextHelper
```

**Returns:** a new helper containing the flattened text with legacy section-sign formatting codes removed

**Since:** 1.8.4

### wrap {#com.jsmacrosce.jsmacros.client.api.helper.TextHelper_wrap-Component-}

```js
static wrap(Component t): TextHelper
```

Wraps a component in a helper, retaining the supplied component as-is.

**Parameters:**
- `t`: the component to wrap, or `null`

**Returns:** a new helper retaining `t`, or `null` when `t` is `null`
