How do I set the number of visible lines in a Gtk::TextView?

故事扮演 提交于 2020-06-23 11:01:56

问题


I have a Gtk::TextView that I would always like to have two lines of text visible, regardless of the font size. Obviously if more than two lines were entered then the box would scroll but I'd like the text view to remain 2 lines tall.

How do I do this?


回答1:


This is very difficult. For example, what will you do if two font sizes are mixed in one line?

One way to do it is to create a Pango layout of one letter and find out its height. This is an untested simplification of some code I wrote in C one time; but it shouldn't be too much trouble to convert it to C++ and GTKmm:

PangoLayout *cell = gtk_widget_create_pango_layout(textview, "X");
int line_height;
pango_layout_get_pixel_extents(cell, NULL, &line_height);
g_object_unref(cell);
gtk_widget_set_size_request(textview, -1, line_height);


来源:https://stackoverflow.com/questions/5631486/how-do-i-set-the-number-of-visible-lines-in-a-gtktextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!