简体   繁体   English

如何使用 Matlab 的 App Designer 将文本包装在标签中

[英]How to wrap text in a label using Matlab's App Designer

I am making a GUI app using Matlab's App Designer.我正在使用 Matlab 的 App Designer 制作一个 GUI 应用程序。 I have a label that I dropped into the GUI that will be for instructions to the user.我有一个标签,我将其放入 GUI 中,用于向用户提供说明。 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.我试过textwrap ,但它给了我一个错误,指出父级不能是标签。

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.要重现,请在 Matlab 的 App Designer 上创建一个新应用程序,将标签拖放到画布上,然后在其中键入一个长文本。 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.Run不会改变它。

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 .您可能不再需要它,但对于与 OP 和我有相同问题的任何人:我已经为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.第一个输入是 uilabel 对象,第二个是您想要作为Text属性的文本。 Because we're creating a figure and deleting it again, it's not super fast (~0.05s to update).因为我们正在创建一个图形并再次删除它,所以它不是非常快(更新约 0.05 秒)。 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.在 uilabel 上直接使用 textwrap 不起作用的原因是因为 textwrap 适用于使用 GUIDE 创建的组件,而不是 App Designer。 There may be an alternative for App Designer, but I'm not aware of it. App Designer 可能有替代方案,但我不知道。

Alternatively, you can use an "Edit Field (Text)" component instead of a label, which should automatically wrap the text.或者,您可以使用“编辑字段(文本)”组件代替标签,标签会自动换行文本。

Thank you very much for your code, for some reason it did not work in my MATLAB 2019b, fixed by adding maximum text width option in this line:非常感谢您的代码,由于某种原因它在我的 MATLAB 2019b 中不起作用,通过在此行中添加最大文本宽度选项来修复:

*outtext = textwrap(h, {txt}, 20);*

20 means maximum text width of 20 characters. 20 表示最大文本宽度为 20 个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM