Tft text screen width

Hello Everyone! I have a 160 pixel wide tft display. I would like to display variable width text with a width of 145 pixels, so that it always falls in the middle. How do you do this? I tried this, but obviously it's not good:

tft.drawString(day, tft.textWidth("50%"), 55, GFXFF);

Other data is displayed on the remaining 15 pixels.

The center of the screen is SCREEN_WIDTH/2
Calculate the length of the text and divide it by two. TEXT_LENGTH/2
Text length will depend on the font size of the text. (* pixels per character)
Start text at SCREEN_WIDTH/2 - (TEXT_LENGTH * PIXELS_PER_CHAR)/2

1 Like

I moved your topic to an appropriate forum category @azxo1 .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thank you very much, it works. "-58" is included in the code because another data appears on the screen there.
SCREEN_WIDTH/2 - (TEXT_LENGTH * PIXELS_PER_CHAR)/2

  myString = nap;     //receive in mqtt
  szoveghossz = myString.length();
  scw = ((tft.width()-58)/2) - ((szoveghossz * 9)/2);   

  tft.setFreeFont(FF1);
  tft.fillRect( 0, 53, 110, 80, TFT_BLACK);
  tft.drawString(nap, scw, 62, GFXFF);
1 Like

Welcome to the fora.

Text length, Hungarian!

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.