# TextFieldWidgetHelper

`com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper`

**Since:** 1.0.5

Wraps the game's text field with a text-change callback.  
 
 The callback receives accepted new text and is attached when the field is built.  
 Scripted [setText(String)](#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setText-String-) changes invoke it when accepted.  
 ```js
const screen = Hud.createScreen("a screen", true);  
screen.setOnInit(JavaWrapper.methodToJava(function (self) {
  const field = self.textFieldBuilder()
     .pos(10, 20)
     .size(120, 14)
     .message("type a name")
     .action(JavaWrapper.methodToJava(function (text) {
       // the callback is given the text, not the field
       Chat.log(`so far: ${text}`);  
     }))
     .build();  

  // Accepted scripted changes invoke the callback too.  
  field.setText("hello");  
  // On 26.1+, rejected text restores the last accepted value, cursor, and selection.  
  field.setTextPredicate(JavaWrapper.methodToJava(function (text) {
    return 8 >= String(text).length;  
  }));  
  field.setText("far too long to be allowed");  
  Chat.log(`the field still says ${field.getText()}`);  
}));  
Hud.openScreen(screen);  
 ```

Available in scripts as `Widgets.TextField`.

## Nested types

- [TextFieldBuilder](TextFieldWidgetHelper.TextFieldBuilder.md)

## Constructors

### new TextFieldWidgetHelper {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_constructor-EditBox-}

```js
new TextFieldWidgetHelper(EditBox t)
```

Wraps an existing text field with a helper whose drawing-order index is zero.

**Parameters:**
- `t`: the text field to wrap

### new TextFieldWidgetHelper {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_constructor-EditBox-int-}

```js
new TextFieldWidgetHelper(EditBox t, int zIndex)
```

Wraps an existing text field with the specified helper drawing-order index.

**Parameters:**
- `t`: the text field to wrap
- `zIndex`: the helper's drawing-order index

## Methods

### getMaxLength {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_getMaxLength-}

```js
getMaxLength(): int
```

Returns the maximum number of UTF-16 code units allowed in the field; a limit of zero
 allows no text.

**Returns:** the maximum number of UTF-16 code units allowed in this text field.

**Since:** 1.8.4

### getSelectedText {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_getSelectedText-}

```js
getSelectedText(): String
```

Returns the selected portion of the text, or an empty string when nothing is
 selected.

**Returns:** the selected text.

**Since:** 1.8.4

### getText {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_getText-}

```js
getText(): String
```

Returns the entered text, excluding the field's suggestion.

**Returns:** the current text; empty when no text is entered.

**Since:** 1.0.5

### isEditable {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_isEditable-}

```js
isEditable(): boolean
```

**Returns:** `true` if the text field is editable, `false` otherwise.

**Since:** 1.8.4

### resetTextPredicate {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_resetTextPredicate-}

```js
resetTextPredicate(): TextFieldWidgetHelper
```

Removes the text filter, allowing all non-null text. Existing text is retained.

**Returns:** self for chaining.

**Since:** 1.8.4

### setCursorPosition {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorPosition-int-}

```js
setCursorPosition(int position): TextFieldWidgetHelper
```

Moves the cursor to a character offset and drops any selection; equivalent to
 calling [setCursorPosition(int, boolean)](#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorPosition-int-boolean-) with `false`.

**Parameters:**
- `position`: the cursor position

**Returns:** self for chaining.

**Since:** 1.8.4

### setCursorPosition {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorPosition-int-boolean-}

```js
setCursorPosition(int position, boolean shift): TextFieldWidgetHelper
```

Moves the cursor to a character offset. With `shift` enabled, the selection
 is extended or shrunk to that position; otherwise it is dropped.

**Parameters:**
- `position`: the cursor position
- `shift`: whether to keep the selection and extend it to the new position

**Returns:** self for chaining.

**Since:** 1.9.0

### setCursorToEnd {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorToEnd-}

```js
setCursorToEnd(): TextFieldWidgetHelper
```

Moves the cursor to the end of the text and drops the selection.

**Returns:** self for chaining.

**Since:** 1.8.4

### setCursorToEnd {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorToEnd-boolean-}

```js
setCursorToEnd(boolean shift): TextFieldWidgetHelper
```

Moves the cursor to the end of the text.  
 With `shift` enabled, the existing selection is extended or shrunk to the end;  
 otherwise it is dropped.

**Parameters:**
- `shift`: whether to keep the selection and extend it to the new position

**Returns:** self for chaining.

**Since:** 1.9.0

### setCursorToStart {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorToStart-}

```js
setCursorToStart(): TextFieldWidgetHelper
```

Moves the cursor to the start of the text and drops the selection.

**Returns:** self for chaining.

**Since:** 1.8.4

### setCursorToStart {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setCursorToStart-boolean-}

```js
setCursorToStart(boolean shift): TextFieldWidgetHelper
```

Moves the cursor to the start of the text.  
 With `shift` enabled, the existing selection is extended or shrunk to the start;  
 otherwise it is dropped.

**Parameters:**
- `shift`: whether to keep the selection and extend it to the new position

**Returns:** self for chaining.

**Since:** 1.9.0

### setEditable {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setEditable-boolean-}

```js
setEditable(boolean edit): TextFieldWidgetHelper
```

Sets whether the field accepts text input. When false, it uses the uneditable
 text color.

**Parameters:**
- `edit`: whether the field should be editable

**Returns:** self for chaining.

**Since:** 1.0.5

### setEditableColor {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setEditableColor-int-}

```js
setEditableColor(int color): TextFieldWidgetHelper
```

Sets the color used to draw text while the field is editable.

**Parameters:**
- `color`: the packed rgb colour to draw editable text in

**Returns:** self for chaining.

**Since:** 1.0.5

### setMaxLength {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setMaxLength-int-}

```js
setMaxLength(int length): TextFieldWidgetHelper
```

Sets the maximum number of UTF-16 code units allowed. If the current text exceeds the
 new limit, it is truncated immediately; a limit of zero allows no text.

**Parameters:**
- `length`: the new maximum number of UTF-16 code units

**Returns:** self for chaining.

**Since:** 1.8.4

### setSelection {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setSelection-int-int-}

```js
setSelection(int start, int end): TextFieldWidgetHelper
```

Selects text between two character offsets: `start` is the cursor position
 and `end` is the other selection endpoint. Offsets are passed to the game
 without validation.

**Parameters:**
- `start`: the offset of the cursor
- `end`: the offset of the other end of the selection

**Returns:** self for chaining.

**Since:** 1.8.4

### setSuggestion {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setSuggestion-String-}

```js
setSuggestion(String suggestion): TextFieldWidgetHelper
```

Sets the hint drawn in the field while it is empty; this does not change the text
 returned by [getText()](#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_getText-).

**Parameters:**
- `suggestion`: the suggestion to set

**Returns:** self for chaining.

**Since:** 1.8.4

### setText {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setText-String-}

```js
setText(String text): TextFieldWidgetHelper
```

Sets the field's text and waits for the change to be applied. Any configured text
 predicate applies, and the callback runs if the text is accepted.

**Parameters:**
- `text`: the text to put in the field

**Returns:** self for chaining.

**Since:** 1.0.5

### setText {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setText-String-boolean-}

```js
setText(String text, boolean await): TextFieldWidgetHelper
```

Sets the field's text, optionally waiting for the change to be applied.  
 
 When called from the client thread there is nothing to hand off and nothing to
 wait for, so the `await` flag makes no difference there. Off the client
 thread the change is queued onto it, and the flag decides whether this call blocks
 until its own task has run. Blocking also waits out whatever else was queued
 ahead of it.  
 
 Any configured text predicate applies to scripted changes; the callback runs for
 accepted changes.

**Parameters:**
- `text`: the text to put in the field
- `await`: whether to wait for the change to be applied

**Returns:** self for chaining.

**Since:** 1.3.1

### setTextPredicate {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setTextPredicate-MethodWrapper-}

```js
setTextPredicate(MethodWrapper<String, ?, ?, ?> predicate): TextFieldWidgetHelper
```

Sets a predicate over the complete proposed text. On 26.1+, rejected input restores
 the last accepted text, cursor, and selection without running the change callback;  
 this is enforced by the responder because `EditBox` has no filter. Earlier
 versions use the field's native filter.

**Parameters:**
- `predicate`: the text filter

**Returns:** self for chaining.

**Since:** 1.8.4

### setUneditableColor {#com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper_setUneditableColor-int-}

```js
setUneditableColor(int color): TextFieldWidgetHelper
```

Sets the color used to draw text while the field is not editable.

**Parameters:**
- `color`: the packed rgb colour to draw uneditable text in

**Returns:** self for chaining.

**Since:** 1.0.5

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

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

Returns a diagnostic string containing the current text, without JSON escaping.

**Returns:** the diagnostic string, formatted as `TextFieldWidgetHelper:{"text": "%s"}`
