uitextarea - Create text area component - MATLAB (original) (raw)

Create text area component

Syntax

Description

`txa` = uitextarea creates a text area in a new figure window and returns the TextArea object. MATLAB® calls the uifigure function to create the figure.

`txa` = uitextarea([parent](#buicnne-1%5Fsep%5Fmw%5Fa6330465-2069-40e4-857c-69da4d660066)) creates the text area in the specified parent container. The parent can be aFigure object or one of its child containers.

example

`txa` = uitextarea(___,[Name,Value](#namevaluepairarguments)) specifies TextArea properties using one or more name-value arguments. For example,uitextarea("Value","Comments") creates a text area that shows the text Comments. Use this option with any of the input argument combinations in the previous syntaxes.

example

Examples

collapse all

Create a text area in a UI figure.

fig = uifigure; txa = uitextarea(fig);

Figure contains an object of type uitextarea.

Create a text area in a UI figure. Specify placeholder text to describe the expected input. The placeholder text shows only when the text area is empty.

fig = uifigure; txa = uitextarea(fig,"Placeholder","Enter feedback");

Figure contains an object of type uitextarea.

Create a populated text area in a UI figure.

fig = uifigure; txa = uitextarea(fig, ... "Value",["First Name Last Name"; "Address 1"; ... "Address 2"; "City, State"; "Postal Code"]);

Figure contains an object of type uitextarea.

Notice that the text area includes a scroll bar so that the app user can view the postal code.

Determine the current width and height of the text area by querying the third and fourth values of the Position property.

Increase the text area size so that all of the content is visible without the use of a scroll bar.

txa.Position(3:4) = [155 80];

Figure contains an object of type uitextarea.

Scroll to the bottom of a text area programmatically.

Create a text area. Specify a size and long text for it.

fig = uifigure; txa = uitextarea(fig); txa.Position = [100 100 80 80]; txa.Value = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.";

Text area in a UI figure window. The text area is vertically scrollable.

Scroll to the bottom of the text area.

Text area in a UI figure window. The text area is scrolled to the bottom.

Create an app that enables a button when an app user enters text in a text area.

In a file named textAreaApp.m, write a function that implements the app:

function textAreaApp fig = uifigure; g = uigridlayout(fig,[3 3]); g.RowHeight = {'fit','fit','fit'}; g.ColumnWidth = {'1x','fit','1x'};

lbl = uilabel(g,"Text","Enter Comments:"); lbl.Layout.Row = 1; lbl.Layout.Column = 2; txa = uitextarea(g); txa.Layout.Row = 2; txa.Layout.Column = 2; btn = uibutton(g,"Text","Submit","Enable","off"); btn.Layout.Row = 3; btn.Layout.Column = 2;

txa.ValueChangedFcn = @(src,event) textEntered(src,event,btn); end

function textEntered(src,event,btn) val = src.Value; btn.Enable = "off"; % Check each element of text area cell array for text for k = 1:length(val) if ~isempty(val{k}) btn.Enable = "on"; break end end end

Run the textAreaApp function. Enter some text in the text area, then click outside the text area to enable the Submit button.

Label, text area, and button in a UI figure window. At the top is a label with text "Enter Comments:". Below is a text area with text "Great app!". At the bottom is a Submit button.

Input Arguments

collapse all

Parent container, specified as a Figure object or one of its child containers: Tab, Panel, ButtonGroup, orGridLayout. If you do not specify a parent container, MATLAB calls the uifigure function to create a new Figure object that serves as the parent container.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: uitextarea(Editable="off") specifies that the app user cannot change the text area text.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: uitextarea("Editable","off") specifies that the app user cannot change the text area text.

Note

The properties listed here are a subset of the available properties. For the full list, see TextArea.

Value changing callback, specified as one of these values:

This callback executes as follows:

If the text area value changes programmatically, then the callback does not execute.

This callback function can access specific information about the user’s interaction with the text area. MATLAB passes this information in a ValueChangingData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.Value is the value in the text area that triggered the execution of the callback. The ValueChangingData object is not available to callback functions specified as character vectors.

Here are the properties of the ValueChangingData object:

Property Description
Value Value that triggered the execution of the callback
Source Component that executes the callback
EventName 'ValueChanging'

The Value property of the TextArea object is not updated until the user either presses Tab or clicks outside the text area. However, you can get the text while the user is still typing by querying the Value property of the ValueChangingData object.

Note

Avoid updating the Value property of theTextArea object from within its ownValueChangingFcn callback, as this might result in unexpected behavior. To update the text area value in response to user input, use aValueChangedFcn callback instead.

For more information about writing callbacks, see Callbacks in App Designer.

Version History

Introduced in R2016a

expand all

Create a ValueChangingFcn callback to program your app to respond when a user is typing in a text area. The callback function executes repeatedly while the user types.

Provide a short hint that describes the expected text area input by using thePlaceholder property.

For more information, see TextArea.

Use the WordWrap property to toggle text wrapping. When theWordWrap property is 'on' (the default), the UI component breaks the text into new lines so that each line fits horizontally within the component. When the property is set to'off', the text does not wrap.

For more information, see TextArea.