TFT.getTextBounds

Hey guys!

Im working with a 2.4 inch TFT screen, connected to a wemos D1 mini.

And I made a function thats draws the text in the center of a given X and Y coordinate.

uint16_t * displayHandler::placeTextInCenter(const String &textBuf, uint16_t x, uint16_t y, uint8_t textSize, uint16_t textColor){
    int16_t x1, y1;
    static uint16_t size[2] = {0, 0};
    tft.setTextSize(textSize);
    tft.setTextColor(textColor);
    tft.getTextBounds(textBuf, x, y, &x1, &y1, &size[0], &size[1]); //calc width of new string
    tft.setCursor(x - size[0] / 2, y - size[1] / 2);
    tft.print(textBuf);
    return size;
}

Credits to this post

when I call this function with setTextSize = 2:

  uint16_t *sizetxt = placeTextInCenter("Setup is done", (DISPLAY_X_MAX / 2), Y_OFFSET + (HEIGHT / 2), 2, ILI9341_BLUE);
  Serial.println(sizetxt[0]);
  Serial.println(sizetxt[1]);

Serial.print:

156 // width
16 // height

The text is neatly in the center.



But when I call this function with a bigger text size, say 3 setTextSize = 3:

  uint16_t *sizetxt = placeTextInCenter("Setup is done", (DISPLAY_X_MAX / 2), Y_OFFSET + (HEIGHT / 2), 3, ILI9341_BLUE);
  Serial.println(sizetxt[0]);
  Serial.println(sizetxt[1]);

Serial.print:

304 // width
48 // height

But here not, somehow it thinks the string "setup is done" is way longer than it really is.

What is going on here?

Hello,

See this if it helps Adafruit_GFX getTextBounds() - adafruit industries

Im not seeing what is going wrong here.

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