Clear Filters
Clear Filters

How do I get my text to center vertically in a uicontrol textbox?

50 views (last 30 days)
I am creating a GUI programmatically and I have a panel that I am displaying text boxes. How do I get my text boxes to display the text in the center of the vertical dimension? There is a horizontalAlignment property that centers horizontally, but the vertical dual does not seem to exist.
hf = figure('Units','normalized',...
'Position',[0.2 0.2 0.6 0.7]);
hPanel = uipanel(hf, 'Position',[0.01 0.02 0.98 0.93]);
posPanel = [0.26 0.28 0.51 0.72];
subPanel = uipanel(hPanel,'Position',posPanel,'FontSize',11);
posTs = [0.2 0.005 0.12 0.08];
SampleTime = uicontrol(subPanel,'Style','text',...
'String','100.0s',...
'FontSize',11,...
'HorizontalAlignment','Center',...
'Units','normalized',...
'Position',posTs,...
'Background',[.8 .8 .8]);
  3 Comments
Jiri Hajek
Jiri Hajek on 28 Jul 2021
A workaround that was sufficient for my needs is that the "Edit Field" component has the text vertically aligned to the center, while "Text Area" has the text vertically aligned to the top...

Sign in to comment.

Answers (1)

Jan
Jan on 7 Mar 2017
You are right: There is no vertical alignment for uicontrol 's. You can create an (invisible) axes instead and use text(), which has the VerticalAlignment property.
  3 Comments
Jorrit M
Jorrit M on 5 May 2021
Rik's solution above works great, but missed the option to align the text left/right, top/bottom, etc. I edited his code below so the function now uses the 'verticalalignment' and 'horizontalalignment' options instead of simply ignoring them and centering everything.
function h=uitext(varargin)
%Mimic a uicontrol call by creating an axes with centered text object.
%
% The input allows all syntax options that uicontrol allows.
% The output is struct('ax',ax_handle,'txt',txt_handle).
%
% All parameters need to be supplied as Name,Value pairs. Below are the
% parameters that are either ignored or redirected to the axes object. The rest
% of the parameters are forwarded to the text object.
%
% ignored:
% Style
% VerticalAlignment
% HorizontalAlignment
% redirected to axes:
% Position
% Units
% Parent
%Deal with the uicontrol(parent,___) syntax.
if mod(nargin,2)==1
varargin=[{'Parent'},varargin];
end
Name=lower({varargin{1:2:end}});
%retrieve alignment parameters
strVertAlign = 'middle';
strHorzAlign = 'center';
vert = ismember(Name,'verticalalignment');
if any(vert)
strVertAlign = varargin{find(vert)*2};
end
horz = ismember(Name,'horizontalalignment');
if any(horz)
strHorzAlign = varargin{find(horz)*2};
end
%Ignore some parameters.
L=ismember(Name,{'style','verticalalignment','horizontalalignment'});
if any(L)
L=find(L);
L=sort([(L*2)-1 L*2]);
varargin(L)=[];
end
%Redirect some parameters to the axes object.
Name=lower({varargin{1:2:end}});
L=ismember(Name,{'position','units','parent'});
if any(L)
L=find(L);L=sort([(L*2)-1 L*2]);ax_args=varargin(L);varargin(L)=[];
else
ax_args={};
end
%Create an axes, initialize the text object, and make the axes invisible.
ax=axes(ax_args{:});axis(ax,[0 1 0 1])
txt=text(0.5,0.5,'','VerticalAlignment',strVertAlign,'HorizontalAlignment',strHorzAlign);
set(ax,'Visible','off')
%Apply the remaining Name,Value pairs.
set(txt,varargin{:});
%Create the ouput struct.
h=struct('ax',ax,'txt',txt);
end
Rik
Rik on 5 May 2021
Thank you for the suggested edits. However, you should also edit the comments and the help text, as that doesn't match the code in your version. (you also moved the first comment to the help text)

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!