GTK#(GTK Sharp 2.12)单选按钮字体

本文关键字:单选按钮 字体 GTK Sharp GTK# | 更新日期: 2023-09-27 18:00:21

我一直遇到这个问题,但如果不是关键问题,我通常会忽略它。我终于需要现在真正解决这个问题,而不是忽视或解决它。基本上就是这样。。。

我找不到更改CheckButton或RadioButton字体的方法。ToggleButton也可能存在此问题,因为前两个都在层次结构中位于它的下面(http://www.mono-project.com/docs/gui/gtksharp/widgets/widget-hierarchy/)而且我也不能更改父按钮小部件的字体。

更改小部件字体的典型方法是使用ModifyFont(…)方法,但在这种情况下显然不起作用。

有人有解决这个问题的工作代码示例吗?我不知所措,我宁愿不写自定义小部件。

谢谢大家。

GTK#(GTK Sharp 2.12)单选按钮字体

编辑

我想明白了。按钮显然没有任何文本,但任何按钮的文本都是一个子标签小部件。radiobutton1.Child.ModifyFont (Pango.FontDescription.FromString ("Comic Sans MS 10"))更改了单选按钮的字体。

上一个答案

我发现了一个粗糙的破解方法,它可以更改绘制的每个小部件的字体:button1.Settings.FontName = "Comic Sans MS 10"设置是一个全局属性,覆盖RC文件信息。不过,这可能没有多大帮助。

我尝试了其他几种更改字体的方法,但没有成功。

button1.PangoContext.FontDescription = Pango.FontDescription.FromString ("Comic Sans MS 10");

button1.ModifierStyle.FontDesc = Pango.FontDescription.FromString ("Comic Sans MS 10");

RcStyle style = new RcStyle ();
style.FontDesc = Pango.FontDescription.FromString ("Comic Sans MS 10");
button1.ModifyStyle (style);

Skyler Brandt成功地回答了我最初的问题,所以这一切都归功于他!我还面临着另外两个问题,我找到了答案,并想在这里发布解决方案,以防其他人需要。

(原始问题已解决,感谢Skyler)更改单选按钮/复选按钮/切换按钮字体

radioButton1.Child.ModifyFont(FontDescription.FromString("whatever"));

(新问题#1,已解决)更改树视图列标题字体

TreeViewColumn column1 = new TreeViewColumn();
Label label1 = new Label("My Awesome Label");
label1.ModifyFont(FontDescription.FromString("whatever"));
label1.Show();
column1.Widget = label1;

(新问题2,已解决)更改常规按钮字体这个有点麻烦,因为按钮的标签在层次结构中是一个很棒的孙子小部件。最简单的方法是为Gtk.Button创建一个扩展方法以便于使用。如果有人有更好的方法,请告诉我。(Skylar指出,他可以使用与单选按钮相同的方法更改常规按钮的字体;然而,我不能。我不确定这是怎么回事。)

public static void ModifyLabelFont(this Gtk.Button button, string fontDescription)
{
    foreach(Widget child in button.AllChildren)
    {
        if(child.GetType() != (typeof(Gtk.Alignment)))
            continue;
        foreach(Widget grandChild in ((Gtk.Alignment)child).AllChildren)
        {
            if(grandChild.GetType() != (typeof(Gtk.HBox)))
                continue;
            foreach(Widget greatGrandChild in ((Gtk.HBox)grandChild).AllChildren)
            {
                if(greatGrandChild.GetType() != (typeof(Gtk.Label)))
                    continue;
                string text = ((Gtk.Label)greatGrandChild).Text;
                ((Gtk.HBox)grandChild).Remove(greatGrandChild);
                Label label = new Label(text);
                label.ModifyFont(FontDescription.FromString(fontDescription));
                label.Show();
                ((Gtk.HBox)grandChild).Add(label);
            }
        }                    
    }
}