edit box properties in app designer
2 次查看(过去 30 天)
显示 更早的评论
Why do you have to set the properties of an app designer edit box object prior to writing text to it for it to render properly? I am trying to display a very long pathname in an edit box of finite width, so I iteratively change the length until a modified version of the filename fits. Here is my code:
function appSetOutputTxt(container, hObject, dspTxt)
s = get(hObject);
% Why do you have to set the edit box properties that you just retrieved in
% order to render text properly?
set(hObject,'FontAngle',s.FontAngle,'FontName',s.FontName,...
'FontSize',s.FontSize,'FontWeight',s.FontWeight);
lstop=false;
pos=get(hObject,'InnerPosition');
strl=pos(3);
edtlength=ceil(strl);
OutString=string(dspTxt);
strlength=length(dspTxt);
while ~lstop
hObject.UserData=dspTxt;
hTest = text(container,'Interpreter','none','Units','pixels',...
'FontAngle',s.FontAngle,'FontName',s.FontName,...
'FontSize',s.FontSize,'FontWeight',s.FontWeight,...
'FontUnits','pixels','String',OutString);
textExt = get(hTest,'Extent');
delete(hTest)
textWidth = ceil(textExt(3)*1.02);
if textWidth>edtlength
strlength=strlength-1;
% if the string is too long, shorten it by one character and try
% again.
OutStringStart=dspTxt(1:floor(strlength*1/3)-3);
OutStringEnd=dspTxt(length(dspTxt)-(floor(strlength*2/3)-1):length(dspTxt));
OutString=strcat(OutStringStart,'...',OutStringEnd);
else
lstop=true;
end
end
set(hObject,'Value',OutString,...
'FontAngle',s.FontAngle,'FontName',s.FontName,...
'FontSize',s.FontSize,'FontWeight',s.FontWeight);
container is an axes object within the app. hObject is the edit box. dspTxt is the full filename we are resizing.
Example:
We start with a filename 'C:\windows\user\someuser\images\savedimages\example\longpath\image.png' and you end up with something like 'C:\windows\...mple\longpath\image.png'
The text doesn't render properly unless the properties that you retrieve with s = get(hObject) are set with the set command. I don't understand why.
4 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!