Skip to content

Commit ba372fe

Browse files
authored
Merge pull request #70 from squix78/fix-multiline-bug
Fix a bug where multiline strings where not correctly centered
2 parents daa91a6 + 662d37b commit ba372fe

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

OLEDDisplay.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,24 +427,24 @@ void OLEDDisplay::drawString(int16_t xMove, int16_t yMove, String strUser) {
427427
// char* text must be freed!
428428
char* text = utf8ascii(strUser);
429429

430-
uint16_t xOffset = 0;
430+
uint16_t yOffset = 0;
431431
// If the string should be centered vertically too
432432
// we need to now how heigh the string is.
433433
if (textAlignment == TEXT_ALIGN_CENTER_BOTH) {
434-
uint16_t lb;
434+
uint16_t lb = 0;
435435
// Find number of linebreaks in text
436-
for (uint16_t i=0, lb=0; text[i]; i++) {
437-
lb += (text[i] == '\n');
436+
for (uint16_t i=0;text[i] != 0; i++) {
437+
lb += (text[i] == 10);
438438
}
439439
// Calculate center
440-
xOffset = (lb * lineHeight) / 2;
440+
yOffset = (lb * lineHeight) / 2;
441441
}
442442

443443
uint16_t line = 0;
444444
char* textPart = strtok(text,"\n");
445445
while (textPart != NULL) {
446446
uint16_t length = strlen(textPart);
447-
drawStringInternal(xMove - xOffset, yMove + (line++) * lineHeight, textPart, length, getStringWidth(textPart, length));
447+
drawStringInternal(xMove, yMove - yOffset + (line++) * lineHeight, textPart, length, getStringWidth(textPart, length));
448448
textPart = strtok(NULL, "\n");
449449
}
450450
free(text);

0 commit comments

Comments
 (0)