kosenko / ui

Boost.UI library
272 stars 20 forks source link

How to center text in label? #4

Closed mohd-akram closed 6 years ago

mohd-akram commented 6 years ago

I want to create a label that fills the width of the window, but with the text centered. Currently, I have this:

#include <boost/ui.hpp>

namespace ui = boost::ui;

int ui_main()
{
    ui::dialog dlg("Title");
    ui::label label(dlg, "h");
    ui::vbox layout(dlg);
    layout << label.layout().justify();
    label.text("hello");
    dlg.show_modal();
    return 0;
}

int main(int argc, char* argv[])
{
    return ui::entry(&ui_main, argc, argv);
}

Using label.layout().justify() fills the space, but causes the text to be left-aligned. Using label.layout().center() centers the element, but causes text to be cutoff when it is changed.

I managed to get it to work with static_cast<wxStaticText*>(label.native_handle())->SetWindowStyleFlag(wxALIGN_CENTRE_HORIZONTAL), but I also came across this issue where I had to set the text to an empty string first, so it would be nice to be able to set this in the constructor.

kosenko commented 6 years ago

I have removed wxST_NO_AUTORESIZE style from constructor and label.layout().center() should work as expected.

Label widget text alignment isn't implemented yet.

mohd-akram commented 6 years ago

Almost there. Now the text doesn't get cutoff when it is changed, but its center position is the previously calculated one (which is a problem if you change the text to a longer or shorter one). I'm not sure how to trigger a relayout.