I am making a GUI app using Matlab's App Designer. I have a label that I dropped into the GUI that will be for instructions to the user. As they proceed, the instruction text lengths will vary. When the text length reaches the end of the label, it gives ...
and cuts off the message. I want the text to wrap inside the label.
I tried textwrap
, but it gave me an error stating that the parent cannot be a label.
To reproduce, create a new app on Matlab's App Designer, drag and drop a label onto the canvas, and type a long text into it. The label size will adjust to let it go off the app window. If you adjust the label width back, it will just cut off the text with ...
, like so:
Pressing Run
doesn't change it.
This is what I want to produce automatically when I change the text to be a string without new lines:
The code I am using to change the text is like this:
methods (Access = private)
% Callback function
function ButtonPushed(app, event)
app.Label.Text = "Lorem ipsum dolor sit amet consectetur adipiscing elit. Vivamus scelerisque nisi ac enim faucib porttitor velit varius. Phasellus luctus ullamcorper nul sit amet finibus neque vehicula ut. Nulla pellentesque.";
end
end
I'm hoping that I don't need to reinvent the wheel and design my own text wrapping function. Is there a method for doing this that has yet to be clarified in the documentation?
You may no longer need this, but for anyone having the same problem as OP and me: I've written a wrapper for textwrap
. Here it is:
function wrapLabelText(label, txt)
% Create a uicontrol whose text will look like that of the label.
h = uicontrol( ...
'Style', 'Text', ...
'Parent', figure('Visible', 'off'), ... % Make sure the containing figure is invisible.
'Position', label.Position, ...
'FontUnits', 'pixels', ... % By default App Designer uses 'pixels' but uicontrol uses 'points'. Define before the FontSize!
'FontSize', label.FontSize, ...
'FontName', label.FontName, ...
'FontAngle', label.FontAngle, ...
'FontWeight', label.FontWeight, ...
'HorizontalAlignment', label.HorizontalAlignment ...
);
% Determine where the text will be wrapped.
outtext = textwrap(h, {txt});
delete(h);
% Assign the text to the label.
label.Text = outtext;
end
The first input is the uilabel object, the second is the text you want as the Text
property. Because we're creating a figure and deleting it again, it's not super fast (~0.05s to update). In your code, you would call this as:
% Callback function
function ButtonPushed(app, event)
txt = 'Lorem ipsum dolor sit amet consectetur adipiscing elit. Vivamus scelerisque nisi ac enim faucib porttitor velit varius. Phasellus luctus ullamcorper nul sit amet finibus neque vehicula ut. Nulla pellentesque.';
wrapLabelText(app.Label, txt);
end
end
The reason using textwrap direcly on the uilabel doesn't work is because textwrap is meant for components created using GUIDE, not App Designer. There may be an alternative for App Designer, but I'm not aware of it.
Alternatively, you can use an "Edit Field (Text)" component instead of a label, which should automatically wrap the text.