diff --git a/lib_nbgl/src/nbgl_obj.c b/lib_nbgl/src/nbgl_obj.c index 451c08328..e07d1b6fc 100644 --- a/lib_nbgl/src/nbgl_obj.c +++ b/lib_nbgl/src/nbgl_obj.c @@ -386,21 +386,32 @@ static void draw_button(nbgl_button_t *obj, nbgl_obj_t *prevObj, bool computePos // draw the text (right of the icon, with 8 pixels between them) if (text != NULL) { nbgl_area_t rectArea; - textWidth = nbgl_getTextWidth(obj->fontId, text); + // Compute available with & height to display the text + rectArea.x0 = obj->obj.area.x0; + rectArea.y0 = obj->obj.area.y0; + // Only one line of text + rectArea.height = nbgl_getFontHeight(obj->fontId); + rectArea.y0 += (obj->obj.area.height - rectArea.height) / 2; + rectArea.width = obj->obj.area.width; if (obj->icon != NULL) { - rectArea.x0 = obj->obj.area.x0 + obj->obj.area.width / 2 - - (textWidth + obj->icon->width + 8) / 2 + obj->icon->width + 8; + rectArea.x0 += obj->icon->width + 8; + rectArea.width -= obj->icon->width + 8; } - else { - rectArea.x0 = obj->obj.area.x0 + (obj->obj.area.width - textWidth) / 2; + // Compute the width & number of characters displayed on first line + uint16_t textLen; + nbgl_getTextMaxLenAndWidth(obj->fontId, text, rectArea.width, &textLen, &textWidth, true); + +#ifdef BUILD_SCREENSHOTS + store_string_infos(text, obj->fontId, &rectArea, true, 1, 1, false); +#endif // BUILD_SCREENSHOTS + + // Center the text, horizontally + if (textWidth < rectArea.width) { + rectArea.x0 += (rectArea.width - textWidth) / 2; } LOG_DEBUG(OBJ_LOGGER, "draw_button(), text = %s\n", text); - rectArea.y0 - = obj->obj.area.y0 + (obj->obj.area.height - nbgl_getFontHeight(obj->fontId)) / 2; - rectArea.width = textWidth; - rectArea.height = nbgl_getFontHeight(obj->fontId); rectArea.backgroundColor = obj->innerColor; - nbgl_drawText(&rectArea, text, nbgl_getTextLength(text), obj->fontId, obj->foregroundColor); + nbgl_drawText(&rectArea, text, textLen, obj->fontId, obj->foregroundColor); } // draw the icon, if any if (obj->icon != NULL) {