textwrap - Wrap text for user interface control - MATLAB (original) (raw)
Wrap text for user interface control
Syntax
Description
Note
Using UI components such as Label or Button objects and wrapping text by setting theWordWrap
property of the component to "on"
is recommended over using the textwrap
function withUIControl
objects.
[wrappedtext](#mw%5F0a15bbc9-68a9-447e-bfee-60e6cc50d616) = textwrap([c](#mw%5F2ccbf894-4a76-411c-b572-1ef7bcb25ce1),[txt](#mw%5Fdd66ad04-50f3-40ad-b31d-58f8a891a9c9))
returns text wrapped at a character width that fits within the specifiedUIControl
object, c
. The UI control object must be one created with the uicontrol
function whose'Style'
property value is set to 'text'
or'edit'
. For example, c = uicontrol('Style','text')
.
[wrappedtext](#mw%5F0a15bbc9-68a9-447e-bfee-60e6cc50d616) = textwrap([c](#mw%5F2ccbf894-4a76-411c-b572-1ef7bcb25ce1),[txt](#mw%5Fdd66ad04-50f3-40ad-b31d-58f8a891a9c9),[numchar](#mw%5Fb8137dff-ff80-4621-9038-727dd1bd0d56))
returns text that wraps each line at the specified number of characters for the given UI control. Spaces are included in the character count. textwrap
avoids splitting words when possible. If a word cannot be accommodated within the specified number of characters, then textwrap
moves it to the start of the next line.
[wrappedtext](#mw%5F0a15bbc9-68a9-447e-bfee-60e6cc50d616) = textwrap([txt](#mw%5Fdd66ad04-50f3-40ad-b31d-58f8a891a9c9),[numchar](#mw%5Fb8137dff-ff80-4621-9038-727dd1bd0d56))
returns text that wraps each line at the specified number of characters.
[[wrappedtext](#mw%5F0a15bbc9-68a9-447e-bfee-60e6cc50d616),[position](#mw%5F211b80ef-3bd7-46d5-9525-c5cde09d2f54)] = textwrap(___)
also returns the recommended position for the UI control based on the text to be wrapped. The returned position is one that allows the full text to display in theuicontrol
without clipping. If a UI control is not specified, the position vector contains all zeros.
Examples
Specify two lines of text for the String
property of a static text field.
c = uicontrol('Style','text'); c.String = {'Extraordinarily long text will be wrapped', ... 'inside of a static text field.'};
The text is cut off and wraps across more than two lines because the default width and height of the text field are not large enough to accommodate the full length of the specified text. Notice how the word "Extraordinarily" is also split across two lines.
Preview the cell array of character vectors that is recommended for wrapping the text so that it fits inside the width of the UI control with the least amount of resizing.
wrappedtext = textwrap(c,c.String)
wrappedtext =
7×1 cell array
{'Extraordinarily'}
{'long text' }
{'will be' }
{'wrapped' }
{'inside of a' }
{'static text' }
{'field.' }
Wrap text at a specified character width and display it in a static text field.
Create a static text field at the default position of [20 20 60 20]
. Specify text to display in it.
c = uicontrol('Style','text'); c.String = {'The data shown represents 18 months of observations.'};
The text is cut off and displays on multiple lines because the default width and height values of the UIControl
object are too small to accommodate the full text.
Preview the wrapped text and the recommended position of theUIControl
object based on a maximum text width of 16 characters.
[wrappedtext,position] = textwrap(c,c.String,16)
wrappedtext =
4×1 cell array
{'The data shown '}
{'represents 18 ' }
{'months of ' }
{'observations.' }
position =
20 20 86 64
Display the wrapped text in the text field and move it to the recommended position.
c.String = wrappedtext; c.Position = position;
Input Arguments
UI control object, specified as a UIControl
object. TheUIControl
object must support multiline text. For instance, itsStyle
property can be 'text'
or'edit'
. Use this argument to determine how text wraps in the specified UI control, or to determine the recommended size for the UI control based on the text to be wrapped.
Text to wrap, specified as a cell array of character vectors, a string array, or a string scalar.
Example: {'Please select an answer from the options below.'}
Example: ["Enter your name using","the format LastName, FirstName"]
Number of characters in each line of text, specified as a positive integer. Use this argument to specify the maximum character width for each line. Ifnumchar
exceeds the number of characters intxt, then the text does not wrap.
Output Arguments
Wrapped text, returned as a cell array of character vectors. To display the text on the specified UI control, you must assign wrappedtext
to theString
property of the UI control.
Position recommended for the UI control, returned as a four-element vector of the form [left bottom width height]
. The units are the same as the units of the UI control. The returned position optimizes the width and height of theuicontrol
so that the specified text can display across multiple lines, without clipping. If a UI control is not specified, then the position vector contains all zeros.
Version History
Introduced before R2006a