简体   繁体   English

如何在vala中更改Gtk.Label的字体大小?

[英]How can I change the font size of a Gtk.Label in vala?

I'm a Vala/Gtk newbie and I'm trying to change the font size of a Gtk.Label, but I can't find a good way to do it. 我是Vala / Gtk的新手,我正在尝试改变Gtk.Label的字体大小,但我找不到一个好方法。

I find out that I can use the markup like this : 我发现我可以使用这样的标记:

    var welcome_message = new Gtk.Label ("<span size='17000'>Hello</span>");
    welcome_message.set_use_markup (true);

But it seems a little hackish. 但它似乎有点hackish。 What is the right way to do it ? 做正确的方法是什么?

Thanks lethalman and nemequ. 感谢lethalman和nemequ。

I think it might help someone so here is a little example of how to use css with Vala. 我认为它可能对某人有所帮助,所以这里是一个如何使用Vala的CSS的小例子。

using Gtk;

public class StyleApp1 : Gtk.Window
{
    public StyleApp1() 
    {

        this.title = "Style app example";
        this.set_border_width (10);
        this.set_position (Gtk.WindowPosition.CENTER);

        this.set_default_size (350, 200);
        this.destroy.connect (Gtk.main_quit);

        var screen = this.get_screen ();
        var css_provider = new Gtk.CssProvider();

        string path = "styleapp1.css";

        // test if the css file exist
        if (FileUtils.test (path, FileTest.EXISTS))
        {
            try {
                css_provider.load_from_path(path);
                Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
            } catch (Error e) {
                error ("Cannot load CSS stylesheet: %s", e.message);
            }
        }

        var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 10);
        this.add (box);

        var label = new Gtk.Label ("Thank you");
        box.add (label);

        var label2 = new Gtk.Label ("Stackoverflow");
        label2.get_style_context().add_class("my_class");
        box.add (label2);
    }
}

static int main(string[] args) {
    Gtk.init(ref args);

    StyleApp1 win = new StyleApp1();
    win.show_all();

    Gtk.main();
    return 0;
}

and the styleapp1.css file : 和styleapp1.css文件:

GtkWindow {
    font-size: 17px;
}

.my_class {
    color: pink;
}

NB : if you use add_provider instead of add_provider_for_screen. 注意:如果您使用add_provider而不是add_provider_for_screen。 You have to use add_provider for every widget that you want to customize. 您必须为要自定义的每个窗口小部件使用add_provider。

You could try with css, I think lately this is the preferred way. 你可以试试css,我想最近这是首选方式。 Give your label a class, then load a css. 给你的标签一个类,然后加载一个CSS。 If you are going to change the font size of a label, I bet you are also going to customize other things so the css may be useful for you. 如果你要改变标签的字体大小,我打赌你也会定制其他东西,所以css可能对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM