SSD1306Ascii icon indicating copy to clipboard operation
SSD1306Ascii copied to clipboard

[Q] Text alignment

Open leikoilja opened this issue 4 years ago • 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

leikoilja avatar Apr 10 '20 08:04 leikoilja

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.

greiman avatar Apr 10 '20 13:04 greiman

As a quick usage example:

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

mtak avatar Feb 10 '21 23:02 mtak

how to center a text vertically?

cvpfus avatar Dec 21 '22 06:12 cvpfus