I tried changing from adafruit_st7735 library to tft_espi, which often error always in this part. What is this code change?
Compilation error: 'class TFT_eSPI' has no member named 'getTextBounds'; did you mean 'setTextFont'?
void drawText(const String& text, int x, int y, uint16_t textColor, uint16_t backgroundColor, uint8_t alpha = DEFAULT_ALPHA, int charSpacing = DEFAULT_CHAR_SPACING) {
int16_t x1, y1;
uint16_t w, h;
// If no character spacing, use original method
if (charSpacing == 0) {
// Get text bounds
tft.getTextBounds(text, x, y, &x1, &y1, &w, &h);
// Add padding for background - INCREASE THIS PADDING
int padding = 4; // Increased from 2 to 4
// Draw background with transparency - CLEAN WIDER AREA
fillRectAlpha(tft, x1 - padding, y1 - padding, w + 2 * padding, h + 2 * padding, backgroundColor, alpha);
// Draw text on top
tft.setTextColor(textColor);
tft.setCursor(x, y);
tft.print(text);
}
else {
// For custom spacing approach
// Get measurements for first character to set font metrics
char firstChar = text.length() > 0 ? text.charAt(0) : 'X';
String firstCharStr(firstChar);
tft.getTextBounds(firstCharStr, 0, 0, &x1, &y1, &w, &h);
// Calculate estimated total width - MAKE THIS WIDER FOR SAFETY
int totalWidth = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
String charStr(c);
int16_t cx1, cy1;
uint16_t cw, ch;
tft.getTextBounds(charStr, 0, 0, &cx1, &cy1, &cw, &ch);
totalWidth += cw + (i < text.length() - 1 ? charSpacing : 0);
}
// Add extra width to ensure full clearing
totalWidth += 10; // Add extra pixels to ensure full clearing
// Draw background area first - INCREASE PADDING
int padding = 4; // Increased from 2 to 4
fillRectAlpha(tft, x - padding, y + y1 - padding, totalWidth + 2 * padding, h + 2 * padding, backgroundColor, alpha);
// Now draw each character with spacing
tft.setTextColor(textColor);
int currentX = x;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
tft.setCursor(currentX, y);
tft.print(c);
// Get width of this specific character
String charStr(c);
int16_t cx1, cy1;
uint16_t cw, ch;
tft.getTextBounds(charStr, 0, 0, &cx1, &cy1, &cw, &ch);
// Move to next character position with spacing
currentX += cw + charSpacing;
}
}
}