How can I align the bottom of two text lables

I’m on v9.0

I’m trying to make two text labels with different font sizes align as if they were written on the same horizontal line. The code I’ve tried is

int align_content_bottom(lv_obj_t *reference, lv_obj_t *alignee) {
    syslog(LOG_ERR, "aligning");
    lv_area_t refarea;
    lv_area_t algnarea;
    lv_obj_update_layout(alignee);
    lv_obj_get_content_coords(reference, &refarea);
    lv_obj_get_content_coords(alignee, &algnarea);
    
    lv_obj_set_y(alignee, lv_obj_get_y(alignee) - (algnarea.y2 - refarea.y2));
    lv_obj_update_layout(alignee);
    return 0;
}

Although the content bottoms are listed as the same, the alignee is always below the reference, regardless of which uses the bigger font. I may be misunderstanding the content area, but how can I find the true bottom of the text which is drawn?

Hey,

You can calculate is the required offset like this:

    lv_obj_t * label1 = lv_label_create(lv_screen_active());
    lv_label_set_text(label1, "Asyb.");
    lv_obj_set_style_text_font(label1, &lv_font_montserrat_40, 0);

    lv_obj_t * label2 = lv_label_create(lv_screen_active());
    lv_label_set_text(label2, "It is a text. ");
    lv_obj_set_style_text_font(label2, &lv_font_montserrat_16, 0);
    lv_obj_set_x(label2, 150);

    int32_t y = lv_font_montserrat_40.line_height - lv_font_montserrat_40.base_line;
    y -= lv_font_montserrat_16.line_height - lv_font_montserrat_16.base_line;
    lv_obj_set_style_translate_y(label2, y, 0);

image

1 Like