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

119 views (last 30 days)
John Petersen
John Petersen on 7 Mar 2017
Commented: Jiri Hajek on 28 Jul 2021
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',...
'Background',[.8 .8 .8]);
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 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.
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.


Community Treasure Hunt

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

Start Hunting!