greiman / SSD1306Ascii

Text only Arduino Library for SSD1306 OLED displays

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Q] Text alignment

leikoilja opened this issue · 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.

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?