# Centre Text Anomaly

**URL:** <https://forum.arduino.cc/t/centre-text-anomaly/1457042>\
**Category:** General Guidance\
**Created:** [August 24, 2026, 12:44pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042 "2026-08-24T12:44:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 12:44pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/1 "2026-08-24T12:44:00Z")

</div>

I have a really frustrating issue...

I am using the adafruit GFX lib with SH1106 driver, and use the centre text helper function shown here previously:

```arduino
void drawCentreString(const char *buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}

```

If I send it a day of the week held in a cont char array:

```arduino
drawCentreString(days_full[tm.tm_wday],64,0);

```

works fine... text is 'counted' as 42.

However, when I try to use something that is not a const char - like:

```arduino
drawCentreString("23rd September",64,10);

```

it places it at the extreme LH side of the display - and 'counts' the text as 124.

(What I want to print is a char array constructed to be the date and month. )

Tearing my hair out!

Any thoughts (please....)

Andy

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [August 24, 2026, 1:03pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/2 "2026-08-24T13:03:32Z")

</div>

> [@alarkham](#):
>
> `uint16_t w, h;`

You might need to change the w and h to int16\_t as

```arduino
display.setCursor(x - w / 2, y);

```

has mixed signed and unsigned math.

Have you set the compiler warnings on?

What does the compiler report?

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [August 24, 2026, 2:40pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/3 "2026-08-24T14:40:36Z")

</div>

> [@alarkham](#):
>
> ```arduino
> drawCentreString("23rd September",64,10);
> 
> ```
> 
> it places it at the extreme LH side of the display - and 'counts' the text as 124.

Does that text fill the entire line of the display?

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 2:41pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/4 "2026-08-24T14:41:13Z")

</div>

Thank you.... compiler gives no errors.

I will try what you suggest, but wht is strange is that it seems to work perfectly when a const char is used, but gives an incorrect width with an ortinary char array....

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 2:42pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/5 "2026-08-24T14:42:52Z")

</div>

Hi David - no its just left justified, as if I hadnt called drawCentreString at all.....

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 24, 2026, 2:59pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/6 "2026-08-24T14:59:02Z")

</div>

> [@alarkham](#):
>
> `"23rd September"`

Try ending this with a null, for example, `"23 September\0"`... something is stepping on the `"x"` value, making it a zero (the left position of your boundary box), causing the "left-justify."

And, maybe try a shorter array of characters, like "23 May" (without "`\0`") to see if the first parameter has limited space for characters.

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 3:14pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/7 "2026-08-24T15:14:52Z")

</div>

Hi XFPD....

So terminating with \0 made no difference.

BUT trying with "23rd" centered perfectly.

So ... why? is there a limit on getTextBounds?

I cant find anything....

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 24, 2026, 3:17pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/8 "2026-08-24T15:17:43Z")

</div>

The thread title is "Centre Text Anomoly" .  
Did you mean "anomaly" instead?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 24, 2026, 3:19pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/9 "2026-08-24T15:19:35Z")

</div>

> [@alarkham](#):
>
> "23rd" centered perfectly.

> [@alarkham](#):
>
> why?

Guessing: Name too long?

Try shortening "September" and all month names, down to three, like "SEP", "MAY", "JUN", "JUL"... and my guess for this is "over-run of array length"... in the particular font you are using (the library probably knows or counts the with of each character)

You can dig into the library to see how it does the math for aligning the boundary box.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 24, 2026, 3:24pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/10 "2026-08-24T15:24:01Z")

</div>

> [@alarkham](#):
>
> If I send it a day of the week held in a cont char array:
> 
> ```arduino
> drawCentreString(days_full[tm.tm_wday],64,0);
> 
> ```
> 
> works fine... text is 'counted' as 42.

For what day of the week did the function return 42?

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 3:25pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/11 "2026-08-24T15:25:47Z")

</div>

Hi b707 - Both Sunday and Monday....

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [August 24, 2026, 3:32pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/12 "2026-08-24T15:32:57Z")

</div>

I think the @xfpd guess is correct. The weekdays names are 6 chars only and returns 42. Your date string is a 14 chars long and does not fit on half of the 128 pixel screen.

To resolve the issue you need to calculate a text width by placing it on a whole width screen

```arduino
void drawCentreString(const char *buf, int x, int y)
{
    int16_t x1, y1;
    uint16_t w, h;
    display.getTextBounds(buf, 0, y, &x1, &y1, &w, &h); //calc width of new string
    display.setCursor(x - w / 2, y);
    display.print(buf);
}

```

note the parameter 0 in the third line of the function

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 24, 2026, 3:35pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/13 "2026-08-24T15:35:04Z")

</div>

> [@b707](#):
>
> weekdays

Try `Wednesday`... and if that causes the left-justify, simplify all month and weekday names to three letters. MON, TUE, WED, THU, FRI, SAT, SUN, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.

Or change font.

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [August 24, 2026, 3:57pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/14 "2026-08-24T15:57:54Z")

</div>

My first thought is that when I'm having a problem figuring something, I don't show a snippet that omits the context in which the problem is occurring.

If I show a sketch that illustrates the problem happening, others can try it for themselves and see if they see the same issue.

For example:

```arduino
#include <Wire.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3c

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
   const char buf[] = "23rd September";
   
   Serial.begin(115200);
   delay(250);
   display.begin(i2c_Address, true);
   display.setTextSize(1);
   display.setTextColor(SH110X_WHITE);
   display.clearDisplay();

   drawCentreString(buf, 64, 10);
   drawCentreString("23rd September", 64, 18);
   display.display();
}

void loop() {}

void drawCentreString(const char *buf, int x, int y) {
   int16_t x1, y1;
   uint16_t w, h;
   display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
   display.setCursor(x - w / 2, y);
   display.print(buf);
   Serial.println(w);
}

```

Which demonstrates that the text width is calculated as 124 for both string literals and a const char buf containing the same text. Thereby demonstrating that the "when I try to use something that is not a const char" was a wild goose chase.

I then might modify the example and discover that a short string literal centred just fine.

I then might gradually increase the length of the string literal until the problem occurred again. And wonder what was special about 11 characters when that length triggered the problem?

Hmm. Default font width is 6 pixels. So 6 x 11 = 66. getTextBounds was asked to find the text bounds of an 11 character string, beginning printing at x=64, and got confused when the string ended up extending past the right hand side of the screen.

Hmm again. I wonder what would happen if I changed the x & y passed to getTextBounds to 0 and 0? `display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h);`

Well, look a that, it centred properly. Now let's try it with the full "23rd September".

Hey that works too. And it works with both a string literal and a const char buffer.

Turns out it was just me using the `getTextBounds` function incorrectly. It was trying (and failing) to calculate the bounds of a text string that _started printing at x=64_ that was longer than the remaining number of pixels left (64\<66).

But that's just me. YMMV

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [August 24, 2026, 3:58pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/15 "2026-08-24T15:58:15Z")

</div>

> [@robtillaart](#):
>
> Have you set the compiler warnings on?

(not answered yet)

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 4:11pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/16 "2026-08-24T16:11:58Z")

</div>

Hi All....

"Wednesday" centres correctly.

So does "23rd Septe" Anything longer left justifies

I am using the default font with the GFX lib - so nothing special.

But more strange behaviour. If I set the x as 0 . the width is counted as 84, and only the end half is visible on the left.

And .. If I set the x as 128 (full width) the width is also cointed as 84, and the first half is displayed on the right, overflowing to the next line!

This is weird.....

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 4:12pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/17 "2026-08-24T16:12:29Z")

</div>

Sorry - i did reply that I had - and no warnings.....

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 4:18pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/18 "2026-08-24T16:18:29Z")

</div>

Hi **[van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)**

Thank you for your help. I didnt include the entire code, as it runs to several hundred lines....

I didnt think of producing a short snippet as you have to demonstrate the problem. I will bear that in mind in future. I have done more testing and will report back...

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 24, 2026, 4:21pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/19 "2026-08-24T16:21:55Z")

</div>

> [@alarkham](#):
>
> "Wednesday"

charlen = 9

> [@alarkham](#):
>
> "23rd Septe"

charlen = 10

> [@alarkham](#):
>
> Anything longer left justifies

maxcharlen = 10

`if (charlen > 10) charlen = 10;`

> [@alarkham](#):
>
> set the x as 0

the math becomes negative, which in computer numbers, could be a HUGE positive number, or wrapped around the screen width at the negative side, and onto the positive side.

---

<div class="post-metadata">

**Author:** ![alarkham](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alarkham/32/1281042_2.png) [@alarkham](https://forum.arduino.cc/u/alarkham)\
**Post date:** [August 24, 2026, 4:39pm UTC](https://forum.arduino.cc/t/centre-text-anomaly/1457042/20 "2026-08-24T16:39:38Z")

</div>

Hi again **van\_der\_decken**

I took your snippet above, and ried what you said..

I wonder what would happen if I changed the x & y passed to getTextBounds to 0 and 0? `display.getTextBounds(buf, 0, 0, &x1, &y1, &w, &h);`

Didnt work for me! Returned a length of 84, but left it centered on 0 - so oly the 2nd half was visible.

Have I missed something?

[Next page](https://forum.arduino.cc/t/centre-text-anomaly/1457042.md?page=2)
