Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Text alignment #67

Open
leikoilja opened this issue Apr 10, 2020 · 3 comments
Open

[Q] Text alignment #67

leikoilja opened this issue Apr 10, 2020 · 3 comments

Comments

@leikoilja
Copy link

leikoilja commented Apr 10, 2020

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 leikoilja changed the title [Q] Centered text [Q] Text alignment Apr 10, 2020
@greiman
Copy link
Owner

greiman commented Apr 10, 2020

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
Copy link

mtak commented Feb 10, 2021

As a quick usage example:

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

@cvpfus
Copy link

cvpfus commented Dec 21, 2022

how to center a text vertically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants