我正在使用MATLAB(2019b)中的App Designer构建GUI。这样的一个方面是允许用户加载文件,然后显示已加载文件的名称。当要显示的文本长于uilabel
大小时,默认行为是截断结尾并附加省略号(“ ...”)。我想从front截断并在其中放置省略号,因为对于用户而言,查看文件名和文件路径更为重要。这是我希望它看起来像的一个示例。
实现此目的的最佳方法是什么?现在,我有一个方法可以测量uilabel
的宽度,然后估算出可以容纳多少个字符并以此为基础截断文本。但是,这似乎并不一致,可能是按比例间距而不是单间距。另外,由于uilabel
的宽度是以像素为单位测量的,所以我担心在其他显示器分辨率和显示环境下它的表现将不一致。有没有比我目前正在做的更好的方法,或者有一种改进我的方法使其更可靠的方法?
示例代码:
classdef sampleApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SelectaFileButton matlab.ui.control.Button
defaultFilename matlab.ui.control.Label
DefaultBehaviorLabel matlab.ui.control.Label
ModifiedBehaviorLabel matlab.ui.control.Label
modifiedFilename matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: SelectaFileButton
function SelectaFileButtonPushed(app, event)
[filename,pathname] = uigetfile('Select a filename to display');
filestr = fullfile(pathname,filename);
% This sets the text the normal way
app.defaultFilename.Text = filestr;
app.defaultFilename.Tooltip = filestr;
% This measures the width of the text field and attempts to fit
% the text to the field
pos = app.modifiedFilename.Position;
nchars = floor(0.165*pos(3))-3; % The 0.165 was found through trial and error
if(length(filestr)>nchars+3)
shortText = ['...' filestr(end-nchars+1:end)];
else
shortText = filestr;
end
app.modifiedFilename.Text = shortText;
app.modifiedFilename.Tooltip = filestr;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 297 111];
app.UIFigure.Name = 'UI Figure';
% Create SelectaFileButton
app.SelectaFileButton = uibutton(app.UIFigure, 'push');
app.SelectaFileButton.ButtonPushedFcn = createCallbackFcn(app, @SelectaFileButtonPushed, true);
app.SelectaFileButton.Position = [13 68 160 22];
app.SelectaFileButton.Text = 'Select a File';
% Create defaultFilename
app.defaultFilename = uilabel(app.UIFigure);
app.defaultFilename.Position = [118 39 133 22];
app.defaultFilename.Text = 'Filename';
% Create modifiedFilename
app.modifiedFilename = uilabel(app.UIFigure);
app.modifiedFilename.Position = [118 19 143 22];
app.modifiedFilename.Text = 'Filename';
% Create DefaultBehaviorLabel
app.DefaultBehaviorLabel = uilabel(app.UIFigure);
app.DefaultBehaviorLabel.HorizontalAlignment = 'right';
app.DefaultBehaviorLabel.Position = [-2 40 110 22];
app.DefaultBehaviorLabel.Text = 'Default Behavior:';
% Create ModifiedBehaviorLabel
app.ModifiedBehaviorLabel = uilabel(app.UIFigure);
app.ModifiedBehaviorLabel.HorizontalAlignment = 'right';
app.ModifiedBehaviorLabel.Position = [-2 20 110 22];
app.ModifiedBehaviorLabel.Text = 'Modified Behavior:';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = sampleApp
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
省略号可能是在系统API(Java)中添加的,matlab不一定可以访问。建议您仅在标签中显示文件名,并使用fileparts提取路径,并将其显示为标签的TooltipString(在R2018中受支持)。