如何使用Pango属性设置标签的字体大小?

时间:2017-12-29 14:15:42

标签: c gtk3 pango

我正在尝试向GTK3窗口添加标签,然后设置标签的字体大小。这是我的尝试:

#include <gtk/gtk.h>

static void
add_label (GtkWidget* window, gchar *text)
{

    GtkWidget *label = gtk_label_new(text);
    PangoAttrList *attrlist = pango_attr_list_new();
    PangoAttribute *attr = pango_attr_size_new_absolute(20);
    pango_attr_list_insert(attrlist, attr);
    gtk_label_set_attributes(GTK_LABEL(label), attrlist);
    pango_attr_list_unref(attrlist);
    gtk_container_add (GTK_CONTAINER (window), label);
}

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
    GtkWidget *window;

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window1");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    add_label( window, "Hello world" );
    gtk_widget_show_all (window);
}

int
main (int argc, char **argv) 
{
    GtkApplication *app;
    int status;

    app = gtk_application_new ( "org.gtk.example", G_APPLICATION_FLAGS_NONE );
    g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref (app);

    return status;
}

这根本不会产生任何标签。如果我注释掉这一行:

gtk_label_set_attributes(GTK_LABEL(label), attrlist);

标签显示,但未设置字体大小。

1 个答案:

答案 0 :(得分:2)

我认为此处缺少PANGO_SCALE

PangoAttribute *attr = pango_attr_size_new_absolute(20 * PANGO_SCALE);

这应该会给你想要的结果:

enter image description here