edit box properties in app designer

2 次查看(过去 30 天)
Thomas
Thomas 2019-1-31
评论: Thomas 2019-2-1
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 个评论
Thomas
Thomas 2019-1-31
编辑:Thomas 2019-1-31
I thought I had the work around with the set command early, but I was wrong. I have to run the function twice for it to set the text properly in the edit box.
Thomas
Thomas 2019-2-1
It seems as though my problem is with the following code
textExt = get(hTest,'Extent');
On the first run when the app is executed, this gives some strange answers. I added the following bit to the while loop above to generate the answers below...
if ~lstop
writestring = [writestring, num2str(textWidth) ', '];
else
writestring = [writestring, num2str(textWidth)];
end
textWidth = 1069, 1061, 1061, 1047, 1040, 1040, 1026, 1023, 1023, 1013, 1005, 1005, 992, 982, 982, 963, 954, 954, 943, 940, 883, 922, 919, 919, 901, 892, 892, 876, 872, 820
So, even though the string gets smaller, the width of the text box gets bigger. (883 length to 922 for example)
The second run makes more sense and performs as expected.
1005, 997, 997, 984, 977, 977, 964, 961, 961, 951, 944, 944, 932, 922, 922, 904, 896, 896, 886, 883, 883, 866, 862, 862, 846, 839, 839, 823
The second time through with the same string (just running the function above from my original question twice) always produces a string that is equal or decreasing in length. Something funky is going on with units or properties here, and I don't know what.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by