011. How to change the font color of GtkLabel?

Problem

You want to change the font color of GtkLabel as shown below:

How to change the font color of GtkLabel?


Solution

Use GtkWidget::modify_fg()

$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse('#366B7E'));

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
<?php
$window = &new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,200);

// create a vbox to hold multiple labels
$vbox = new GtkVBox();

// create an eventbox that allows you set background color
$eventbox = new GtkEventBox();
$eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00'));

// create the title label and stuff it in the eventbox
$title = new GtkLabel('This is the title');
$title->set_size_request(100,48);
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse('#366B7E'));  // note 1
$title->modify_font(new PangoFontDescription('Times New Roman Italic 24'));
$vbox->pack_start($eventbox, 0, 0); // note 2
$eventbox->add($title);

// create the body_text and place it below the title
$label2 = new GtkLabel('This is the body text');
$vbox->pack_start($label2);

$window->add($vbox);
$window->show_all();
Gtk::main();
?>

Output

As shown above.
 

Explanation

The font color of a GtkLabel is changed using modify_fg (note 1).

The font and font size is changed using modify_font (note 2).

Add comment


Security code
Refresh