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

Strange behaviour when using display.getTextBounds #234

Open
crazynightgriffin opened this issue Mar 2, 2022 · 1 comment
Open

Strange behaviour when using display.getTextBounds #234

crazynightgriffin opened this issue Mar 2, 2022 · 1 comment

Comments

@crazynightgriffin
Copy link

crazynightgriffin commented Mar 2, 2022

Hi When using

If the text is over 10 characters it does not centre. code a simple example.

`#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {

Serial.begin(9600);
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
}

void loop() {
// put your main code here, to run repeatedly:

display.setTextSize(1);
display.clearDisplay();
display.setTextColor(WHITE);
drawCentreText("aaaaaaaaaaa", 70, 20);   //writes 2nd row of text
display.display();
delay(5000);

}
// this is a helper loop and centers the text
void drawCentreText(const char *buf, int x, int y)
{
int16_t x1;
int16_t y1;
uint16_t w;
uint16_t h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor(x - w / 2, y);
display.print(buf);
}`

@electroman00
Copy link

electroman00 commented Jun 29, 2022

[OLED Display Module 12864]
https://www.amazon.com/gp/product/B0837DLWVH/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1

drawCentreText("1234567890123456789012",62 ,32 ); //Center on Display

will center 123456789012345678901

// 21 Characters w/setTextSize(1)
// 10 Characters w/setTextSize(2)
// 7 Characters w/setTextSize(3)
// 5 Characters w/setTextSize(4)

display.getTextBounds(buf, 0, y, &x1, &y1, &w, &h); //calc width of new string

hth

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

2 participants