How to Center text on OLED
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- mbigos
- Posts: 7
- Joined: Wed Jan 04, 2023 11:17 pm
How to Center text on OLED
Anyone have a easy way to center text to written to a display using arduino IDE? I found display.getTextBounds but I haven't found a good example of how to use it.
- mikeysklar
- Posts: 14194
- Joined: Mon Aug 01, 2016 8:10 pm
Re: How to Center text on OLED
@david_prentice has a helpful post on this:
call with:
https://forum.arduino.cc/t/adafruit-ole ... t/617181/5
Code: Select all
void drawCentreString(const char *buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor(x - w / 2, y);
display.print(buf);
}
Code: Select all
if (Input == 1) drawCentreString("ONE", 13, 12);
else if (Input == 2) drawCentreString("TWO", 13, 12);
else if (Input == 3) drawCentreString("THREE", 13, 12);
...
- mbigos
- Posts: 7
- Joined: Wed Jan 04, 2023 11:17 pm
Re: How to Center text on OLED
Thanks I got it working for the text.
Please be positive and constructive with your questions and comments.