/*
Forum: https://forum.arduino.cc/t/best-way-to-print-letters-with-strikethrough/1355282
Wokwi: https://wokwi.com/projects/423427538942706689
2025-02-20
ec2021
Realization of a simple "Strike Through" function for TFT displays
Text can wrap at the borders
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
tft.begin();
tft.setRotation(0);
}
void loop() {
for (int i = 1; i < 6; i++) {
Print("Strike Through Strike Through", 10, 10, i, ILI9341_RED);
delay(2000);
tft.fillScreen(ILI9341_BLACK);
}
}
void Print(char * txt, int atX, int atY, int tSize, uint16_t col) {
tft.setCursor(atX, atY);
tft.setTextColor(col);
tft.setTextSize(tSize);
tft.println(txt);
int charHeight = getCharHeight();
int height = getTotalTxtHeight(txt, atX, atY);
int width = getTotalTxtWidth(txt, atX, atY);
int noOfLines = (height + 1) / charHeight;
int i = 0;
while (width > 0) {
tft.drawFastHLine(atX, atY + charHeight / 2 + i * charHeight, width, col);
width = width - (tft.width() - atX);
atX = 0;
i++;
}
};
int getCharHeight() {
int x, y, width, height;
tft.getTextBounds("C", 0, 0, &x, &y, &width, &height);
return height;
}
int getTotalTxtWidth(char * txt, int atX, int atY) {
int boxX, boxY, width, height;
tft.setTextWrap(false);
tft.getTextBounds(txt, atX, atY, &boxX, &boxY, &width, &height);
tft.setTextWrap(true);
return width;
}
int getTotalTxtHeight(char * txt, int atX, int atY) {
int boxX, boxY, width, height;
tft.getTextBounds(txt, atX, atY, &boxX, &boxY, &width, &height);
return height;
}