cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a Text to be align centered vertically ?

Nzhan.15
Associate II

Assuming the height of the drawable object is H (getHeight()), and the height of text is TH (getTextHeight()), how to figure out the correct y value of the text object ??

I tried (getHeight() - text.getTextHeight() - text.getTypedText().getFont->getSpacingAbove()) / 2 and (getHeight() - text.getTextHeight() ) / 2, neither of them is right.

15 REPLIES 15
Martin KJELDSEN
Chief III

Hi,

The calculation seems right to me. Can you show me, visually, what's wrong with your calculation?

/Martin

Martin KJELDSEN
Chief III

Can you debug and verify that the numbers are correct? (text height and spacing above)

/Martin

0690X000008i889QAA.png

It is obviously that, the up padding is different from down padding,

and I figure the padding through (getHeight() - text.getTextHeight() - fontSpaceAbove) / 2

The value is wrong !!! value of the getHeight() is 44, while the textHeight() is 34 and the fontspaceabove is 5, thus, the padding is (44-34-5) / 2 = 2, it seems wrong!!!

Might be more effecient if you simply send me your project, if that's possible.

/Martin

It was fixed, the true height of the text is  text.getTypedText().getFont->getSpacingAbove() + text.getTypedText().getFont()->getMaxTextHeight()

Will this work if the text in question is inside a textArea? I would like my text to center it vertically inside a textArea and there is no way to do this in the Designer (a horizontal centering is provided, I suggest adding vertial alignment capability to the Designer in future updates).

I am having issues gaining access the typedText of the textArea because it is "protected".

Hi @scottSD​,

The text is always inside a TextArea. The original posters issue was centering the whole thing properly, vertically because getTextHeight() does not, in all cases, give you the height of the textarea itself.

Martin,

Thanks for the reply.

Is there away to minimize the height of the textArea so that it is very close to the height

of the actual text? When I turn Auto-size off and reduce the height of the textArea, it does reduce it, but brings the bottom edge up to the text and not the top edge. Reducing the height some more clips the bottom of the text and it is no longer vertically centered (all of this done using the Designer):

0690X00000984JoQAI.png

0690X00000984K3QAI.png

The reason I would like to do this is to reduce the invalidated rectangle as it is next to other items.

I assume this is because of a top spacing requirement in the font?