Change the default font size of TextView without css

I want to change the default font size of TextView, i use the following code to achieve this.

provider = gtk_css_provider_new ();
gtk_css_provider_load_from_string (provider,
                                 "textview {"
                                 " font-size: 22px;"
                                 "}");
 context = gtk_widget_get_style_context (text_view);
 gtk_style_context_add_provider (context,
                                GTK_STYLE_PROVIDER (provider),
                                GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

It works fine, however, when i compile it, the compiler warns me that
ā€˜gtk_widget_get_style_context’ and ā€˜gtk_style_context_add_provider’ have been deprecated. I don’t know why gtk delete the function so fast even it appears in official gtk4 document.
I know maybe it can been achieved by using css file, but is it possible to achieve it without css?

I think only setting the provider globally is supported now. Try using gtk_style_context_add_provider_for_display instead.

Only setting a provider on the style context of a widget has been deprecated, because it does not work like people expect it to—the style rules do not cascade into the children of a widget.

If you want to load custom style rules, you should always use gtk_style_context_add_provider_for_display(), which has not been deprecated.

They are not ā€œdeletedā€: they’ll continue to be available until GTK4 is EOL, which will happen when GTK6 is released—and we haven’t even started working on GTK5 at the moment.

The deprecation warnings are there to help during the port across major versions of GTK; if you know you’re using deprecated functions, you can easily disable the warnings until you want to port your code away from them.

You can create a text tag with its own font description, and apply it to the whole content of the text buffer.

1 Like

Thank you for replying, I have successfully solved this problem. :grinning:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.