Borrar filtros
Borrar filtros

Matlab AppDesigner - determine Label size automatically by FontSize

17 visualizaciones (últimos 30 días)
Aleksandar Ritan
Aleksandar Ritan el 10 de En. de 2021
Editada: Katherine May el 28 de Abr. de 2022
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 comentario
Katherine May
Katherine May el 29 de Mzo. de 2022
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.

Iniciar sesión para comentar.

Respuestas (1)

Katherine May
Katherine May el 28 de Abr. de 2022
Editada: Katherine May el 28 de Abr. de 2022
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');
end
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
end
function redrawMaskHelp(app)
try
if(app.reWrapText)
if(verLessThan('matlab', '9.9'))
%drawnow;
[wrappedText, ~] = wrapTextWorkAround(app, app.descText);
set(app.descText, 'Text', wrappedText); % Automatic text wrapping not supported until Matlab 2020b
end
app.reWrapText = false;
end
catch e
disp(e);
end
end
function [wrappedText, position] = wrapTextWorkAround(app, labelObj)
type = get(labelObj, 'Type');
if(~strcmp(type, 'uilabel'))
wrappedText = '';
position = [0, 0, 0, 0];
return;
end
text = get(labelObj, 'Text');
if(iscell(text))
position = get(labelObj, 'Position');
wrappedText = text;
else
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});
delete(out);
end
if(iscell(wrappedText))
wrappedText = strjoin(wrappedText, newline);
end
end

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by