greiman/SSD1306Ascii

[Q] Text alignment

leikoilja opened this issue · 3 comments

Hey, @greiman!

Thank you for amazing library!

I am using a simple SSD1306 screen with you library and was wondering if there is a method/example how to print text centered or aligned to the right/left edge?
For example something like oled.print("Hello world", align="center")` maybe not specifically like that, but something similar?

Thanks in advance

You can center a text string by using this function:
size_t SSD1306Ascii::strWidth(const char* str)
It returns the width of a string in pixels.

Subtract the string width from the screen width and use setCursor so half of the pixels are before the string.

mtak commented

As a quick usage example:

  size_t size = oled.strWidth(instrumentNameBuffer);
  oled.setCursor((oled.displayWidth()-size)/2, 0);
  oled.println(instrumentNameBuffer);

how to center a text vertically?