Search code examples
tizentizen-wearable-sdktizen-native-app

How can I center vertically a label?


I'm developing a native app for Samsung Gear (Tizen). I have a multi line label dynamically generated (can have 2 or 3 lines). How can I center it vertically?


Solution

  • Use Elementary Box container. It align childs in vertical middle.

    here is sample code. refer code for box and 3 labels in app_create function.

    #include <sstream>
    
    #include <app.h>
    #include <Elementary.h>
    #include <efl_extension.h>
    
    static bool app_create(void *data)
    {
        Evas_Object *win = elm_win_util_standard_add("sample", "sample");
        elm_win_autodel_set(win, EINA_TRUE);
    
        if (elm_win_wm_rotation_supported_get(win)) {
            int rots[4] = { 0, 90, 180, 270 };
            elm_win_wm_rotation_available_rotations_set(win, (const int *)(&rots), 4);
        }
    
        auto back_cb = [](void *data, Evas_Object *obj, void *event_info) {
            elm_win_lower(static_cast<Evas_Object*>(data));
        };
    
        evas_object_smart_callback_add(win, "delete,request",
                [](void*, Evas_Object*, void*){ui_app_exit();}, NULL);
        eext_object_event_callback_add(win, EEXT_CALLBACK_BACK, back_cb, win);
    
        Evas_Object *conform = elm_conformant_add(win);
        elm_win_indicator_mode_set(win, ELM_WIN_INDICATOR_SHOW);
        elm_win_indicator_opacity_set(win, ELM_WIN_INDICATOR_OPAQUE);
        evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
        elm_win_resize_object_add(win, conform);
        evas_object_show(conform);
    
        Evas_Object *box = elm_box_add(conform);
        evas_object_show(box);
        elm_object_content_set(conform, box);
    
        Evas_Object *labels[3];
    
        for (int i=0; i<3; i++)
        {
            std::ostringstream ss;
            ss << "<align=center>line : " << i << "</align>";
            labels[i] = elm_label_add(box);
            elm_object_text_set(labels[i], ss.str().c_str());
            evas_object_show(labels[i]);
            elm_box_pack_end(box, labels[i]);
        }
    
        evas_object_show(win);
        return true;
    }
    
    
    int main(int argc, char *argv[])
    {
        ui_app_lifecycle_callback_s event_callback = {
                app_create, nullptr, nullptr, nullptr, nullptr
        };
        return ui_app_main(argc, argv, &event_callback, nullptr);
    }