How to Center text on OLED

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mbigos
 
Posts: 7
Joined: Wed Jan 04, 2023 11:17 pm

How to Center text on OLED

Post by mbigos »

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.

User avatar
mikeysklar
 
Posts: 14194
Joined: Mon Aug 01, 2016 8:10 pm

Re: How to Center text on OLED

Post by mikeysklar »

@david_prentice has a helpful post on this:

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);
}
call with:

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);
    ...
https://forum.arduino.cc/t/adafruit-ole ... t/617181/5

User avatar
mbigos
 
Posts: 7
Joined: Wed Jan 04, 2023 11:17 pm

Re: How to Center text on OLED

Post by mbigos »

Thanks I got it working for the text.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”