How to dynamically update the size of the text field based on the size of the dialog window in MATLAB R2022b?

12 views (last 30 days)
I am trying to fit really long messages into a help dialog box that has customized width and height. So far changing the size of the dialog box using the 'Position' property has no effect on how the message is displayed. Is there a way to dynamically update the size of the text field based on the size of the dialog window?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Apr 2023
By design, ‘msgbox’ (and thus ‘helpdlg’) is supposed to automatically wrap the message to fit an appropriately sized figure so we could expect call to ‘helpdlg’ to do the best possible job of creating a right-sized figure window with text properly placed in it. 
If you use the 'Position' property, the 'Position' of the ‘helpdlg’ is updated because it is a figure window, and the 'Position' property is part of the figure. As expected, the size and position of the figure (dialog) window get updated when the 'Position' property is updated. 
However, the placement of the text within the dialog is not automatically adjusted. This is because of the internal working of the text field in ‘helpdlg’ (‘msgbox’). By design, to create the text field within the dialog, ‘uicontrol’ (with style text) with fixed units in points is used. Therefore, the size of the text field is determined during the dialog creation time calculated based on the text that it needs to include, and updating it later is not possible. 
A possible workaround would be to use the ‘gridlayouts’ property in ‘uifigure’ to achieve dynamic updating of the fields based on the figure size. Here is one example using 'uilabel' component:
% Create figure for designing Apps
fig = uifigure('Position',[100 100 437 317]);
% Create a grid layout manager
g = uigridlayout(fig, [1 1]);
% Create some long text strings
str1 = '11111111111111111111111111111111111111111111111111111';
str2 = '22222222222222222222222222222222222222222222222222222';
str3 = '33333333333333333333333333333333333333333333333333333';
str=[str1 str2 str3];
% Create a UIlabel component
aText = uilabel(g,'Text',str);
% Use WordWrap to wrap the text in the label
aText.WordWrap = "on";
With the 'WordWrap' property turned on for the 'uilabel', whatever sized figure we have, the text attempts to adjust dynamically. You can try dragging the figure to increase its size or try adjusting the 'Position' property to see the dynamic update in effect. 
You can find more information on the ‘GridLayout’ properties following the link below: 
Here is another useful resource that explains how to lay out UI components inside figure programmatically: 
More information on UI components can be found here:

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!