2017-06-06 98 views
1

我有一個GTK標籤,我在它Arial Rounded Mt Bold通過使用下面的代碼顯示的文字:如何使用兩種不同的字體在GTK標籤上顯示文本?

PangoFontDescription *df; 
df = pango_font_description_new(); 
pango_font_description_set_family(df,"Arial Rounded Mt Bold"); 
pango_font_description_set_size(df,fontsize*PANGO_SCALE); 
gtk_widget_modify_font(Message_Label, df); 
gtk_label_set_text(GTK_LABEL(Label), "Hello World"); 
pango_font_description_free (df); 

現在這個Hello World顯示在Arial Rounded Mt Bold。但是,如果我想在Arial Rounded Mt Bold和World中以某種其他字體顯示Hello,例如Arial。這在GTK標籤中是可行的嗎?我在C做任何建議或任何有用的鏈接。謝謝。

回答

4

gtk_widget_modify_font()已棄用,不會讓你做你想做的。

您可以使用PangoAttrList,它在文本範圍內組合屬性(包括PangoFontDescriptor的各個組件)。例如:

PangoAttrList *attrlist; 
PangoAttribute *attr; 
PangoFontDescription *df; 

attrlist = pango_attr_list_new(); 

// First, let's set up the base attributes. 
// This part is copied from your code (and slightly bugfixed and reformatted): 
df = pango_font_description_new(); 
pango_font_description_set_family(df, "Arial Rounded MT"); 
pango_font_description_set_size(df, fontsize * PANGO_SCALE); 
pango_font_description_set_weight(df, PANGO_WEIGHT_BOLD); 
// You can also use pango_font_description_new_from_string() and pass in a string like "Arial Rounded MT Bold (whatever fontsize is)". 
// But here's where things change: 
attr = pango_attr_font_desc_new(df); 
// This is not documented, but pango_attr_font_desc_new() makes a copy of df, so let's release ours: 
pango_font_description_free(df); 
// Pango and GTK+ use UTF-8, so our string is indexed between 0 and 11. 
// Note that the end_index is exclusive! 
attr->start_index = 0; 
attr->end_index = 11; 
pango_attr_list_insert(attrlist, attr); 
// And pango_attr_list_insert() takes ownership of attr, so we don't free it ourselves. 
// As an alternative to all that, you can have each component of the PangoFontDescriptor be its own attribute; see the PangoAttribute documentation page. 

// And now the attribute for the word "World". 
attr = pango_attr_family_new("Arial"); 
// "World" starts at 6 and ends at 11. 
attr->start_index = 6; 
attr->end_index = 11; 
pango_attr_list_insert(attrlist, attr); 

// And finally, give the GtkLabel our attribute list. 
gtk_label_set_attributes(GTK_LABEL(Label), attrlist); 
// And (IIRC this is not documented either) gtk_label_set_attributes() takes a reference on the attribute list, so we can remove ours. 
pango_attr_list_unref(attrlist); 

您還可以使用gtk_label_set_markup()use an HTML-like markup language同時包含文字和樣式設置一氣呵成:

gtk_label_set_markup(GTK_LABEL(Label), 
    "<span face=\"Arial Rounded MT\" size=\"(whatever fontsize * PANGO_SCALE is)\" weight=\"bold\">Hello <span face=\"Arial\">World</span></span>"); 
// Or even... 
gtk_label_set_markup(GTK_LABEL(Label), 
    "<span font=\"Arial Rounded MT Bold (whatever fontsize is)\">Hello <span face=\"Arial\">World</span></span>"); 
相關問題