Sample Code 5: How to right justify the text in GtkLabel? |
|
Written by kksou
|
|
Tuesday, 12 September 2006 |
|
Problem You want to right justify the text in GtkLabel as shown below:

Solution Use GtkLabel::set_justify()
$label->set_justify(Gtk::JUSTIFY_RIGHT);
Sample Code 1 2 3 4 5 6 7
| <?php $window = new GtkWindow(); $window->set_size_request(400, 100); $window->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#B2D2DE")); $window->connect_simple('destroy', array('Gtk','main_quit')); $label = new GtkLabel("hello world!\nthis is line2\nand this is line3"); $label->set_justify(Gtk::JUSTIFY_RIGHT);
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- A label can contain multiple lines by using
\n as line breaks. Note that you have to enclose \n in double quotes, not single quotes.
- To right justify, use
Gtk::JUSTIFY_RIGHT
- To left justify, use
Gtk::JUSTIFY_LEFT
- To center text, use
Gtk::JUSTIFY_CENTER
- To left-right justify text, use
Gtk::JUSTIFY_FILL
Notes
Note that $label->set_justify() only justifies the text within the label. The entire label is still centered relative to the window.
If you wish to produce something similar to the output below, i.e. the whole text right align to the right of GtkWindow:

Please refer to this article.
User reviews Average user ratings: 4.0 (from 2 users) Note: You have to be a registered member to leave a comment. Free registration here. |
August 06, 2008 2:43pm
March 06, 2009 3:23pm