Property "Extent" does not contain the correct size

I want to put some text in a GUI, and I want to know the exact size of a uicontrol type 'text' !

I found several threads explaining that this can be done using the 'Extent' property of the 'text' object containing the same text, see example:

 function form = tempfunc(txt,font,fontsize) if nargin <3 fontsize = 10; end if nargin < 2 font = 'courier'; end f = figure('Visible','off','Units','pixels'); u = uicontrol(f,'Style','text','Units','pixels','String',txt,'FontName',font,'FontSize',fontsize); textsize = get(u,'Extent'); textsize = textsize(3:4); close(f); form = figure('Units','pixels'); uicontrol(form,'Style','text','Units','pixels','String',txt,'FontName',font,'FontSize',fontsize,'Position',[5,5,textsize]); end 

My problem is that this does not work. When I run above using tempfunc(repmat('A',14)); , I get the following window with a picture:

enter image description here

As you can see from the image, the height of the text extracted in the textsize variable is too small!

Please note that this is the result that I received when I started the program on my Windows computer on which Matlab R2014a is installed. Then I ran the same code on the Linux machine running Matlab R2013b, and on this machine I got the result that I wanted.

The GUI that I create should (hopefully) be able to run on any computer, but right now I'm really at a loss about how I should make a version that works on any machine, so please help me!

EDIT: I tried to run the same code on another computer running Windows 7 (this time Ultimate edition instead of my Enterprise version), which runs Matlab R2011b (instead of my R2014a), it still creates the wrong height of the text box, but on this time the text box was too high - see image:

enter image description here

EDIT2: I finally got R2014b, but unfortunately this did not help! I have a similar picture:

enter image description here

I also tried to understand if different resolution options for my screen had changed - they did not.

EDIT3:

I noticed that different fonts give different errors in height, for example. the default font (MS Sans Serif) gives the text field too high (and this error in height also grows as more lines are added). However, on Linux, I got the correct result for all the fonts I tried.

But in fact, I am most interested in the case of using a courier font, because for my purpose I need a monospace font.

+6
source share
1 answer

Overseeing the Java part, Swing components have several interesting ways:

The fact is that the "preferred size" seems to be the correct size (which you are looking for), while the size returned by get(...,'Extent'); is the size of visible , which has the following meaning

getVisibleRect ()

Returns the "visible rectangle" component - the intersection of this component, the visible rectangle, the new rectangle (0, 0, getWidth (), getHeight ()) and all visible rectangles of its ancestors .

To clarify the above: themed and platform decorations of the figure window can reduce the component's available space and, therefore, its visible size (as mentioned here ).

As a numerical example, when I start with the default settings and repmat('A',14) I get (on Win7, MATLAB 2015a):

  • get(u,'Extent') - [0,0,116,214]
  • jHandle.getVisibleRect - java.awt.Rectangle[x=0,y=0,width=116,height=214]
  • jHandle.getSize - java.awt.Dimension[width=116,height=214]
  • jHandle.getPreferredSize - java.awt.Dimension[width=116,height=221]

Now the question is, how to get PreferredSize (or jHandle from which it can be removed) conveniently ...

One option I used is findjobj , which is as easy to use as jHandle = findjobj(u) .

Summarizing:

  • Put findjobj in your working folder.
  • Replace the two lines where you will find textsize as follows:

    v = findjobj (u); textize = [v.getPreferredSize.getWidth v.getPreferredSize.getHeight];

  • PROFIT.

PS

My reasoning may be wrong, and Swing's understanding is wrong, however this explanation makes sense to me, and more importantly, it works.

+1
source

Source: https://habr.com/ru/post/982040/


All Articles