Skip to main content
Nzhan.15
Associate III
June 3, 2019
Question

How to make a Text to be align centered vertically ?

  • June 3, 2019
  • 15 replies
  • 4863 views

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.

This topic has been closed for replies.

15 replies

Martin KJELDSEN
Principal III
June 3, 2019

Hi,

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

/Martin

Nzhan.15
Nzhan.15Author
Associate III
June 3, 2019

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

Martin KJELDSEN
Principal III
June 3, 2019

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

/Martin

Nzhan.15
Nzhan.15Author
Associate III
June 3, 2019

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!!!

sasha
Associate III
January 31, 2020

Hi, I am also trying to achieve vertical text centering. Did you get this working? I can't get this line of code to compile:

text.getTypedText().getFont()->getSpacingAbove() + text.getTypedText().getFont()->getMaxTextHeight()

I get these errors:

'no matching function for call to 'touchgfx::Font::getSpacingAbove() const'

'no matching function for call to 'touchgfx::Font::getMaxTextHeight() const'

I have included Font.hpp, but this function is declared as Virtual and there seems to be no other implementation.

How did you get this working? I'm sure I'm missing something obvious here...

scottSD
Senior III
March 30, 2020

I am not sure if you still need this as your post is 2 months old, but the functions getSpacingAbove() and getMaxTextheight() require text to be passed to them. I casted some characters to const Unicode::UnicodeChar* and the compiler did not complain:

 int spacingAbove = text.getTypedText().getFont()->getSpacingAbove((const Unicode::UnicodeChar*)"TEXT");

 int maxHeight = text.getTypedText().getFont()->getMaxTextHeight((const Unicode::UnicodeChar*)"TEXT");