Class TextField
A text input component that allows users to enter and edit text.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class TextField : ExVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, IInputElement<string>, IValidatableElement<string>, INotifyValueChanging<string>, INotifyValueChanged<string>
Remarks
TextField is a fundamental input component that enables users to enter and edit text content. It provides a versatile interface for capturing user input in various formats like plain text, passwords, and validated content.
The component supports different sizes, placeholder text, leading and trailing icons, a static suffix such as a unit, and various customization options to fit different use cases and design requirements.
When using TextField for sensitive information like passwords, make sure to enable the isPassword property to mask the input appropriately.
Examples
Basic TextField Example: Simple text input with placeholder.
<TextField placeholder="Enter your name" size="Md" />
Password Field Example: Secure password input with validation.
<TextField
is-password="true"
mask-char="*"
placeholder="Enter password"
max-length="20"
leading-icon-name="lock"
/>
Search Field Example: Search input with icons.
<TextField
leading-icon-name="search"
trailing-icon-name="clear"
placeholder="Search..."
size="Lg"
/>
Suffix Example: a unit rendered after the input.
<TextField placeholder="Width" suffix="px" />
Append-friendly Example: keep the caret where the user clicked.
<TextField select-all-on-focus="false" />
Validated Input Example: Email input with validation.
var textField = new TextField();
textField.placeholder = "Enter email";
textField.validateValue = (value) => {
return value.Contains("@") && value.Contains(".");
};
Constructors
TextField()
Default constructor.
Declaration
public TextField()
TextField(string)
Construct a TextField with a predefined text value.
Declaration
public TextField(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | A default text value. |
Remarks
No event will be triggered when setting the text value during construction.
Fields
inputContainerUssClassName
The TextField input container styling class.
Declaration
public const string inputContainerUssClassName = "appui-textfield__inputcontainer"
Field Value
| Type | Description |
|---|---|
| string |
inputUssClassName
The TextField input styling class.
Declaration
public const string inputUssClassName = "appui-textfield__input"
Field Value
| Type | Description |
|---|---|
| string |
leadingContainerUssClassName
The TextField leading container styling class.
Declaration
public const string leadingContainerUssClassName = "appui-textfield__leadingcontainer"
Field Value
| Type | Description |
|---|---|
| string |
leadingIconUssClassName
The TextField leading icon styling class.
Declaration
public const string leadingIconUssClassName = "appui-textfield__leadingicon"
Field Value
| Type | Description |
|---|---|
| string |
placeholderUssClassName
The TextField placeholder styling class.
Declaration
public const string placeholderUssClassName = "appui-textfield__placeholder"
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The TextField size styling class.
Declaration
public const string sizeUssClassName = "appui-textfield--size-"
Field Value
| Type | Description |
|---|---|
| string |
suffixUssClassName
The TextField suffix styling class.
Declaration
public const string suffixUssClassName = "appui-textfield__suffix"
Field Value
| Type | Description |
|---|---|
| string |
trailingContainerUssClassName
The TextField trailing container styling class.
Declaration
public const string trailingContainerUssClassName = "appui-textfield__trailingcontainer"
Field Value
| Type | Description |
|---|---|
| string |
trailingIconUssClassName
The TextField trailing icon styling class.
Declaration
public const string trailingIconUssClassName = "appui-textfield__trailingicon"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The TextField main styling class.
Declaration
public const string ussClassName = "appui-textfield"
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
The content container of the TextField.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
invalid
The invalid state of the TextField.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool invalid { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
isPassword
Whether the TextField is a password field.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool isPassword { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
isReadOnly
Whether the TextField is read-only.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool isReadOnly { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
leadingElement
The TextField leading element.
Declaration
public VisualElement leadingElement { get; set; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
leadingIconName
The leading icon name.
Declaration
[CreateProperty]
[UxmlAttribute]
public string leadingIconName { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
maskChar
The TextField mask character.
Declaration
[CreateProperty]
[UxmlAttribute]
public char maskChar { get; set; }
Property Value
| Type | Description |
|---|---|
| char |
maxLength
The TextField max length.
Declaration
[CreateProperty]
[UxmlAttribute]
public int maxLength { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
placeholder
The TextField placeholder text.
Declaration
[CreateProperty]
[UxmlAttribute]
public string placeholder { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
selectAllOnFocus
Whether focusing the field selects its whole value, so that typing replaces it.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool selectAllOnFocus { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Enabled by default, matching the rest of the design system: a field you tab into is ready to be overwritten. Turn it off for fields users are expected to append to.
size
The TextField size.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
suffix
A short static text rendered after the input, such as a unit or a currency symbol.
Declaration
[CreateProperty]
[UxmlAttribute]
public string suffix { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
The suffix sits between the input and the trailing element, is purely decorative and never takes focus. It is hidden entirely when null or empty, so it costs no layout space on a plain field. The string is localized, like every other App UI text.
trailingElement
The TextField trailing element.
Declaration
public VisualElement trailingElement { get; set; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
trailingIconName
The trailing icon name.
Declaration
[CreateProperty]
[UxmlAttribute]
public string trailingIconName { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
validateValue
The validation function for the TextField.
Declaration
[CreateProperty]
public Func<string, bool> validateValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<string, bool> |
value
The TextField value.
Declaration
[CreateProperty]
[UxmlAttribute]
public string value { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
GetSizeUssClassName(Size)
Declaration
public static string GetSizeUssClassName(Size enumValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | enumValue |
Returns
| Type | Description |
|---|---|
| string |
SetValueWithoutNotify(string)
Set the TextField value without notifying the change.
Declaration
public void SetValueWithoutNotify(string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | newValue | The new value of the TextField. |