greiman / SSD1306Ascii

Text only Arduino Library for SSD1306 OLED displays
MIT License
504 stars 121 forks source link

[Q] Text alignment #67

Open leikoilja opened 4 years ago

leikoilja commented 4 years ago

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

greiman commented 4 years ago

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 3 years ago

As a quick usage example:

  size_t size = oled.strWidth(instrumentNameBuffer);
  oled.setCursor((oled.displayWidth()-size)/2, 0);
  oled.println(instrumentNameBuffer);
cvpfus commented 1 year ago

how to center a text vertically?