Matlab AppDesigner - determine Label size automatically by FontSize

21 ビュー (過去 30 日間)
Aleksandar Ritan
Aleksandar Ritan 2021 年 1 月 10 日
編集済み: Katherine May 2022 年 4 月 28 日
I'm writing the UIFigureSizeChanged callback for my GUI. The problem I'm having is with the labels and their font size. I would like to change the font size according to the window size, but also change the size of the box holding the Label automatically according to the font size. What I mean by the box is the 3rd and 4th value in the Position parameter, that is Position(3:4).
When you manually change the Label font size in the AppDesigner, the Label box becomes bigger or smaller to fit the Label text exactly. But, when I change the Label font size from the code, the box holding the label stays the same and the label is cut out (if the new font is bigger than the previous font). Is there a way to calculate the Label box size from the font size and the text the Label contains? How does the AppDesigner calculate it automatically in the Design View?
  1 件のコメント
Katherine May
Katherine May 2022 年 3 月 29 日
In GUIDE, this was covered by the UIControl 'Extent' property. The Update Guide says there's no equivalent in App Designer. I'm trying to figure out a workaround myself.


回答 (1 件)

Katherine May
Katherine May 2022 年 4 月 28 日
編集済み: Katherine May 2022 年 4 月 28 日
If you're using Matlab R2020b+, AppDesigner has automatic text wrapping which works very well. You can set it as a property of the Label:
if(~verLessThan('matlab', '9.9'))
set(app.descText, 'WordWrap', 'on');
If you're using Matlab R2020a or earlier, I found a workaround:
  1. create an old-style figure() with a label of the same dimensions & font size as the AppDesigner label,
  2. use that figure() to wrap the text, and then
  3. copy the wrapped text back to your AppDesigner label.
My app makes use of AppDesigner's dynamic grids, which will change the length of your app objects based on their content size; however, text wrapping (adds line breaks) still needs to be done in figure() until R2020b. You can make use of the position return value if you don't want to use dynamic grids.
properties (Access = private)
reWrapText = true; % If text wrapping needs to be recalculated (uicontrol stuff is temporally expensive!)
fig = figure('Visible', 'off'); % Dummy figure for uicontrol workarounds - keeping a figure open saves lots of time
function redrawMaskHelp(app)
if(verLessThan('matlab', '9.9'))
[wrappedText, ~] = wrapTextWorkAround(app, app.descText);
set(app.descText, 'Text', wrappedText); % Automatic text wrapping not supported until Matlab 2020b
app.reWrapText = false;
catch e
function [wrappedText, position] = wrapTextWorkAround(app, labelObj)
type = get(labelObj, 'Type');
if(~strcmp(type, 'uilabel'))
wrappedText = '';
position = [0, 0, 0, 0];
text = get(labelObj, 'Text');
position = get(labelObj, 'Position');
wrappedText = text;
font = get(labelObj, 'FontName');
fontSize = get(labelObj, 'FontSize');
textPos = get(labelObj, 'Position');
maskPos = get(app.mask, 'Position');
set(app.fig, 'Position', maskPos);
out = uicontrol(app.fig, 'Style', 'text', 'String', text, 'FontName', font, 'FontSize', fontSize, 'FontUnits', 'pixels', 'Position', textPos);
[wrappedText, position] = textwrap(out, {text});
wrappedText = strjoin(wrappedText, newline);


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by