I want to place some text in a GUI, and I want to know the exact size the uicontrol
of type 'text'
needs to be!
I've found several threads explaining that this can be done using the 'Extent'
property of a '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 now that this doesn't work.
When I run the above with tempfunc(repmat('A',14));
I get the following figure window:
As you can see from the image the height of the text extracted in the textsize
variable is too small!
Note that this is the result I got when I ran the program on my Windows computer running Matlab R2014a. I later ran the exact same code on a Linux machine running Matlab R2013b, and on that machine I got the result I wanted.
The GUI I am making should (hopefully) be able to run on any computer, but right now I am really at a loss on how I should proceed to make a version that works on any machine, so please help me!
EDIT: I tried to run the same code on another Windows 7 machine (this time Ultimate edition instead of my Enterprise edition) running Matlab R2011b (instead of my R2014a), it still produced the wrong height of the text box - but this time the text box was too high - see image:
EDIT2: I finally got R2014b installed, but sadly it did not help! I got a similar looking picture:
I also tried to see if different choices of resolution of my screen made any difference - they did not.
EDIT3:
I've noticed that different fonts yield different errors in the height, e.g. the default font (MS Sans Serif) yields a text box that is too high (and this error in height also grows as more lines get added) - On Linux however I got the correct result for all the fonts I tried.
But really the case I am most interested in is the case using the courier font, since I need a monospaced font for my purpose.
tempfunc(repmat('A',14));
is replaced bytempfunc(repmat('A',5));
or some other number, the amount of extra or missing vertical space varies. The more tests I do, the more it looks like a bug? – Gorget